Home > Back-end >  To change the property for part of a cell value on Excel by Python
To change the property for part of a cell value on Excel by Python

Time:08-02

Is it possible to change the property for part of a cell value (like: set "Demo" only to Bold letters of a cell value "Demo Data") on Excel by Python? thanks.

CodePudding user response:

from openpyxl.styles import Font
from openpyxl import Workbook
wb = Workbook()
ws = wb.active
a1 = ws['A1']
a1.value = 'Demo Data'  # set value
ft = Font(bold=True)   # set bold 
a1.font = ft  # apply
wb.save('font.xlsx')  # save

CodePudding user response:

if you are looking to make part of a cell bold while other part remains normal, you can use xlsxwriter. Here is an example below... Note that there are many other changes like color, etc. Refer to documentation here.

import xlsxwriter
workbook = xlsxwriter.Workbook('Sample.xlsx')
worksheet = workbook.add_worksheet()
#worksheet.set_column('A:A', 9)
bold = workbook.add_format({'bold': True})
segments = [bold, 'Demo ', ' data']
worksheet.write_rich_string('A1', *segments)
workbook.close()
  • Related