finally successful in ods with python, just one help needed.
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.
from odf.opendocument import OpenDocumentSpreadsheet
from odf.style import Style, TableColumnProperties, ParagraphProperties
from odf.table import Table, TableRow, TableColumn, \
from odf.text import P
ods = OpenDocumentSpreadsheet()
col = Style(name='col', family='table-column')
centered = Style(name='centered', family='table-cell')
table = Table()
# Add first row with cell spanning columns A-C
tr = TableRow()
tc = TableCell(numbercolumnsspanned=3, stylename=centered)
# Add two more rows with non-spanning cells
for r in (2,3):
tr = TableRow()
for c in ('A','B','C'):
tc = TableCell()
tc.addElement(P(text='%s%d' % (c, r)))
if __name__ == "__main__":
More information about the Python-list