Home > Net >  How to put Style in the middle of a string?
How to put Style in the middle of a string?

Time:08-30

Is it possible?

I'm using Openpyxl.

It's okay if it's a different library

EX: enter image description here

Try:

size_9_center_font = Font(name='Consolas', size=9, bold=True)
ws['A2'].font = title_font

CodePudding user response:

Openpyxl doesn't have released option to do this currently but you can do it with Xlwings using the characters attribute.

import xlwings as xw


wb = xw.Book('foo.xlsx')
ws = wb.sheets('Sheet1')

ws.range('A2').value = "I want to put the style in the string"

ws.range('A2').font.size = 9
ws.range('A2').font.name = 'Consolas'

ws.range('A2').characters[10:13].font.bold = True
ws.range('A2').characters[18:23].font.color = (255, 0, 0)

wb.save()
wb.close()
  • Related