
As described in an earlier article called ODF is a timesaver!, you can easily create ODF documents without even having the huge OpenOffice installed on your machine. This can save a lot of time. I got very interested in this topic and did some research around how you can use Python scripts to generate ODF.
After a bit of googling I found a python module called ODFPY. This package allows you to do lots of fantastic things with ODF using Python which is my favourite language.
After installing the package I found a bunch of examples which was extremely useful because I got a starting point to hack my way forward.
After tinkering a bit with the examples I come up with this basic code:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from odf.opendocument import OpenDocumentText
from odf.style import PageLayout, MasterPage, Header, Footer, Style, TextProperties
from odf.text import P
textdoc = OpenDocumentText() # init main document
pl = PageLayout(name="pagelayout") # creating a basic page layout
textdoc.automaticstyles.addElement(pl) # adding the layout to the text document
mp = MasterPage(name="Standard", pagelayoutname=pl) # creating a masterpage
textdoc.masterstyles.addElement(mp) # applying the masterpage to text document
h = Header() # let's create a header!
hp = P(text="My header")
f = Footer() # let's create a footer
fp = P(text="My footer")
pageText=P(text="Hello world!", stylename=boldstyle) # some text for our page!
h.addElement(hp) # connecting elements
f.addElement(fp)
mp.addElement(h)
mp.addElement(f)
textdoc.text.addElement(pageText)
textdoc.save("mydocument.odt") # saving everything in a file
There's much more to discover. The API seems to be very detailed. So I've already found a thing to tinker with next weekend :)






