Search code examples
pythonlibreofficeodfpy

Merge cells with odfpy


I created a Libre Office spreadsheet with odfpy:

from odf.opendocument import OpenDocumentSpreadsheet
from odf.table import Table, TableRow, TableCell
from odf.text import P

document = OpenDocumentSpreadsheet()
table = Table(name="Table1")
document.spreadsheet.addElement(table)

tr = TableRow()
table.addElement(tr)
cell = TableCell(stylename="some style")
cell.addElement(P(text="very very very very long text"))
tr.addElement(cell)

document.save("file.ods")

The cell I created contains a long text. I would like to merge the cell with the neighbor cell, so the text fits in. Is that possible? I could not find an example. I know that I can set the width of the columns, but that's not what I want.


Solution

  • I now found out how to merge cells: you can set numbercolumnsspanned and numberrowsspanned for a TableCell:

    cell = TableCell(numberrowsspanned=2, numbercolumnsspanned=3)