Rich Text & Comments API
Rich Text
Rich Text allows a single cell to contain multiple text segments, each with its own font formatting (e.g., mixing bold, italic, and different colors within one cell).
from pyopenxlsx import Workbook, XLRichText, XLRichTextRun, XLColor
wb = Workbook()
ws = wb.active
# 1. Create a Rich Text object
rt = XLRichText()
# 2. Add normal text run
run1 = XLRichTextRun("Normal text, ")
rt.add_run(run1)
# 3. Add styled text runs
run2 = XLRichTextRun("Bold Red")
run2.bold = True
run2.font_color = XLColor(255, 0, 0)
rt.add_run(run2)
run3 = XLRichTextRun(", and ")
rt.add_run(run3)
run4 = XLRichTextRun("Italic Blue.")
run4.italic = True
run4.font_color = XLColor(0, 0, 255)
rt.add_run(run4)
# 4. Assign the XLRichText object directly to the cell's value
ws["A1"].value = rt
wb.save("richtext.xlsx")
Comments
Cell comments are notes attached to individual cells.
pyopenxlsxautomatically resizes comment boxes to fit the text perfectly.