ODFPy documentation

20.6k Views Asked by At

I need to manipulate the ODF file format (open document format, the open office's internal format), and I need to do it in Python.

It seem's ODFPy is a wonderful library for this purpose. Unfortunately the official documentation is very poor, almost unuseful. I can't find almost anything online - maybe it is not so popular?

Is there anyone who can point me at some some information or better documentation?

6

There are 6 best solutions below

1
On

There's a good example of odfpy usage at http://mashupguide.net/1.0/html/ch17s04.xhtml

2
On

try ezodf they also have a doc

0
On

It's outdated, a bit, but could help someone. I have found only one way to work with ODFPY:

  1. generate your ODF document (i.e. f1.ods)
  2. make copy of it and edit in LibreOffice/OpenOffice or other (i.e. f2.odf)
  3. change both files to f1.zip and f2.zip
  4. extract both files.

main formatting and data is located in "content.xml" and "styles.xml"

  1. compare both formatting
  2. make changes in python script
  3. 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.

0
On

I found more documentation (the web site has been reorganized in the past few years) in api-for-odfpy.odt.

3
On

The documentation is unfortunately horrible, and the generated Python wrapper is lousily documented in code, providing lots of functions whose argument lists look like func(*args).

The reference manual is actually useful, but not when you're starting out - it doesn't provide any context of how to use these functions. I would suggest starting with the tutorial and all the examples. Even though they may have nothing to do with your use case, they will help you get the feel of how the package works. After you've gotten used to the way the package is structured, you can often make sense of the documentation by combining the API doc with the information in the OpenDocument Essentials book.

(The relationship is somewhat tenuous at best, but you can often intuit method and attribute values from it. When working with the spreadsheet, for example, the handy list of office:value-type data in the book provided the necessary constants for building proper TableCell(valuetype=...) instances)

Also, making small documents in OpenOffice and then inspecting the xml and comparing it to the XML generated from ODFPy greatly helps you debug where you might have gone wrong.

1
On

Okay, here's a quick help:

  1. Grab odfpy source code:

    ~$ svn checkout https://svn.forge.osor.eu/svn/odfpy/trunk odfpy
    
  2. Install it:

     ~$ cd odfpy
     ~/odfpy$ python setup.py install
    
  3. Generate the documentation:

    ~/odfpy$ epydoc --pdf odf
    

    I have uploaded the generated documentation here.

  4. Run this simple example program:

    from odf.opendocument import OpenDocumentText
    from odf.text import P    
    textdoc = OpenDocumentText()
    p = P(text="Hello World!")
    textdoc.text.addElement(p)
    textdoc.save("helloworld", True)
    
  5. Read the examples and try to make sense of everything:

    ~/odfpy$ emacs examples/*.py
    

Hope that helps! Good luck!