I\'m using Python version 3.6 and the latest version of the openxlpy module (v2.4.8) on Windows.
I want to change certain font to bold in a cell, but I don\'t want a
Answers post title but not ops specific question.
from openpyxl.workbook import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
ws['B3'] = "Hello"
ws['B3'].font = Font(bold=True)
wb.save("BoldDemo.xlsx")
I do not think openpyxl has implemented any handling for intracell style differences. If you take a file that has multiple styles in one cell, import it in openpyxl and save it new file, without changing the cell, the new file will lose its formatting.
>>>import openpyxl
>>>path = 'test.xlsx'
>>>book = openpyxl.load_workbook(path)
>>>book.active['a1']
>>>book.active['a1'].value
'not bold line\nbold line\nnot bold line\n'
>>>print(_)
not bold line
bold line
not bold line
>>>book.save(path)
From here you have a couple options.
Use another lib for handling xlsx. I will refrain from talking about using alternative libraries because I'm presuming you are using openpyxl for a reason.
Create your own markdown and use a custom script to add the formatting after editing and saving the file with openpyxl. You can find the spec for Office Open XML File Formats here. Here is a also a great article about parsing xlsx files and xml.
edit the codebase for openpyxl.
If the steps above are not achievable/desirable, try to contact the openpyxl maintainers, or hire help :/