pythonodfpy

How to use bold text inside a spreadsheet using odfpy?


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


Solution

  • 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")