It's outdated, a bit, but could help someone.
I have found only one way to work with ODFPY:
- generate your ODF document (i.e. f1.ods)
- make copy of it and edit in LibreOffice/OpenOffice or other (i.e. f2.odf)
- change both files to f1.zip and f2.zip
- extract both files.
main formatting and data is located in "content.xml" and "styles.xml"
- compare both formatting
- make changes in python script
- iterate 1-7 until you have sufficient result :D:D
Here is some date-time format example, I have made that way:
from odf.opendocument import OpenDocumentSpreadsheet
from odf.style import Style, TableCellProperties
from odf.number import DateStyle, Text, Year, Month, Day, Hours, Minutes, Seconds
from odf.text import P
from odf.table import Table, TableRow, TableCell
# Generate document object
doc = OpenDocumentSpreadsheet()
table = Table(name="Exported data")
#create custom format in styles.xml
date_style = DateStyle(name="date-style1") #, language="lv", country="LV")
date_style.addElement(Year(style="long"))
date_style.addElement(Text(text=u"-"))
date_style.addElement(Month(style="long"))
date_style.addElement(Text(text=u"-"))
date_style.addElement(Day(style="long"))
date_style.addElement(Text(text=u" "))
date_style.addElement(Hours(style="long"))
date_style.addElement(Text(text=u":"))
date_style.addElement(Minutes(style="long"))
date_style.addElement(Text(text=u":"))
date_style.addElement(Seconds(style="long", decimalplaces="3"))
doc.styles.addElement(date_style)
#link to generated style from content.xml
ds = Style(name="ds1", datastylename="date-style1",parentstylename="Default", family="table-cell")
doc.automaticstyles.addElement(ds)
#create simple cell
tr = TableRow()
tc = TableCell(valuetype='string')
tc.addElement(P(text = "Date-Time"))
tr.addElement(tc)
table.addElement(tr)
#create cell with custom formatting
lineDT = #some date-time variable
tr = TableRow()
tc = TableCell(valuetype='date',datevalue = lineDT.strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3],stylename=ds)
tc.addElement(P(text=lineDT.strftime("%Y-%m-%d %H:%M:%S.%f")[:-3]))
tr.addElement(tc)
table.addElement(tr)
#save ods
doc.spreadsheet.addElement(table)
doc.save("test.ods", True)
I have updated code, a bit, because previous version opened wrong on MS product.