Editing Workbooks with Rich Text in Openpyxl

Editing workbooks with rich text in openpyxl

Formatting below the level of the cell is not supported by openpyxl. To use it you'd have to implement your own code when writing as openpyxl just stores whatever strings it receives. Full read/write support would add a great deal of complexity.

how to save the new excel with the same style in every cell using openpyxl

Every cell in openpyxl has a .style attribute that you can call and set. Your code would be this:

from openpyxl import load_workbook,Workbook
wb = load_workbook("test11.xlsx",data_only=True)
sheetnames = wb.sheetnames
for name in sheetnames:
ws = wb.get_sheet_by_name(name)
print(ws)
wb2 = Workbook()
ws2 = wb2.active
for i,row in enumerate(ws.iter_rows()):
for j,cell in enumerate(row):
c = ws2.cell(row=i+1, column=j+1, value=cell.value)
c.style = cell.style
ws2.title = name
wb2.save(name + ".xlsx")

How to keep style format unchanged after writing data using OpenPyXL package in Python?

According to the answer to this question, you can format cells in Excel using openpyxl.

The answer given there only changes the target cell to bold, but maybe you can change the font face back to lemons1.

from openpyxl.workbook import Workbook
from openpyxl.styles import Font
wb = Workbook()
ws = wb.active
ws['B3'] = "Hello"
ws['B3'].font = Font(name='lemons1', size=14)
wb.save("FontDemo.xlsx")

However, according to the documentation, you can only apply styles to whole cells, not to part of a cell. So you would need to put the Khmer characters in one cell and the English characters in another cell.



Related Topics



Leave a reply



Submit