ODFPY

Lawrence D'Oliveiro ldo at geek-central.gen.new_zealand
Mon Jun 7 00:30:58 EDT 2010


Very handy library for creating and manipulating ODF documents without any
dependency on OpenOffice.org <http://odfpy.forge.osor.eu/>.

I’ve been looking at using it for automating the generation of the invoices
I send out at the end of each month. So far, it’s been a lot easier than
trying to figure out PyUNO. :)

One thing, though, the examples are full of procedural sequences involving
creating an object, and then calling some other object’s addElement method
to add the new object as a child—lots of repetitive accesses to intermediate
variables. So I wrote this nice, simple convenience routine

    def AddElement(Parent, Construct, Attrs, Children) :
        # convenience routine for building ODF structures.
        NewElt = Construct(**Attrs)
        for Child in Children :
            NewElt.addElement(Child)
        #end for
        if Parent != None :
            Parent.addElement(NewElt)
        #end if
        return NewElt
    #end AddElement

With this, I can build quite complicated structures in a single statement,
using a more functional approach, e.g.

    AddElement \
      (
        Parent = TheDoc.text,
        Construct = odf.text.P,
        Attrs = dict
          (
            stylename = AddElement
              (
                Parent = TheDoc.automaticstyles,
                Construct = odf.style.Style,
                Attrs = dict(name = "work header", family = "paragraph"),
                Children =
                    (
                        odf.style.TextProperties(fontweight = "bold"),
                        AddElement
                          (
                            Parent = None,
                            Construct = odf.style.ParagraphProperties,
                            Attrs = dict
                              (
                                marginbottom = "0.21cm"
                              ),
                            Children =
                                (
                                    AddElement
                                      (
                                        Parent = None,
                                        Construct = odf.style.TabStops,
                                        Attrs = dict(),
                                        Children =
                                            (
                                                odf.style.TabStop(position = "15.1cm"),
                                            )
                                      ),
                                )
                          ),
                    )
              )
          ),
        Children =
            (
                odf.text.Span(text = "Description of Work"),
                odf.text.Tab(),
                odf.text.Span(text = "Charge"),
            )
      )




More information about the Python-list mailing list