Skip to content
Advertisement

How to use bold text inside a spreadsheet using odfpy? [closed]

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

Advertisement

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")
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement