How to put Style in the middle of a string?

Question:

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
Asked By: hacoo

||

Answers:

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['A2'].value = "I want to put the style in the string"

ws['A2'].font.size = 9
ws['A2'].font.name = 'Consolas'

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

wb.save()
wb.close()

Answered By: moken
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.