finally successful in ods with python, just one help needed.

David Bolen db3l.net at gmail.com
Sat Mar 14 23:35:55 CET 2009


Krishnakant <hackingkk at gmail.com> writes:

> based on your code snippid I added a couple of lines to actually center
> align text in the merged cell in first row.

Sorry, guess I should have verified handling all the requirements :-)

I think there's two issues:

* I neglected to add the style I created to the document, so even in my
  first example, columns had a default style (not the 1in) style I thought
  I was creating.

* I don't think you want a paragraph style applied to the paragraph
  text within the cell, but to the cell as a whole.  I think if you
  just try to associate it with the text.P() element the "width" of
  the paragraph is probably just the text itself so there's nothing to
  center, although that's just a guess.

I've attached an adjusted version that does center the spanned cell
for me.  Note that I'll be the first to admit I don't necessarily
understand all the ODF style rules.  In particular, I got into a lot
of trouble trying to add my styles to the overall document styles
(e.g., ods.styles) which I think can then be edited afterwards rather
than the automatic styles (ods.automaticstyles).

The former goes into the styles.xml file whereas the latter is included
right in contents.xml.  For some reason using ods.styles kept causing
OpenOffice to crash trying to load the document, so I finally just went
with the flow and used automaticstyles.  It's closer to how OO itself
creates the spreadsheet anyway.

-- David

    from odf.opendocument import OpenDocumentSpreadsheet
    from odf.style import Style, TableColumnProperties, ParagraphProperties
    from odf.table import Table, TableRow, TableColumn, \
                          TableCell, CoveredTableCell
    from odf.text import P

    def make_ods():
        ods = OpenDocumentSpreadsheet()

        col = Style(name='col', family='table-column')
        col.addElement(TableColumnProperties(columnwidth='1in'))

        centered = Style(name='centered', family='table-cell')
        centered.addElement(ParagraphProperties(textalign='center'))

        ods.automaticstyles.addElement(col)
        ods.automaticstyles.addElement(centered)

        table = Table()
        table.addElement(TableColumn(numbercolumnsrepeated=3, stylename=col))
        ods.spreadsheet.addElement(table)

        # Add first row with cell spanning columns A-C
        tr = TableRow()
        table.addElement(tr)
        tc = TableCell(numbercolumnsspanned=3, stylename=centered)
        tc.addElement(P(text="ABC1"))
        tr.addElement(tc)

        # Add two more rows with non-spanning cells
        for r in (2,3):
            tr = TableRow()
            table.addElement(tr)
            for c in ('A','B','C'):
                tc = TableCell()
                tc.addElement(P(text='%s%d' % (c, r)))
                tr.addElement(tc)

        ods.save("ods-test.ods")

    if __name__ == "__main__":
        make_ods()



More information about the Python-list mailing list