How to use bold text inside a spreadsheet using odfpy?

392 views Asked by At

I'm writing a spreadsheet (ods) using odfpy but I can't figure out how to make it use bold text for a cell.

1

There are 1 answers

0
Joril On BEST ANSWER

You have to define a style for the "table-cell" family, for example:

from odf import opendocument
from odf.table import Table, TableRow, TableCell
from odf.style import Style, TextProperties
from odf.text import P

basedoc = opendocument.OpenDocumentSpreadsheet()
boldstyle = Style(name="BoldStyle", family="table-cell")
boldstyle.addElement(TextProperties(attributes={"fontweight": "bold"}))
basedoc.styles.addElement(boldstyle)

sheet = Table(name="Test")
tablerow = TableRow()
cell = TableCell(valuetype="string", stylename=boldstyle)
cell.addElement(P(text="bold cell"))
tablerow.addElement(cell)
sheet.addElement(tablerow)
basedoc.spreadsheet.addElement(sheet)
basedoc.save("test.ods")