Andrej Koelewijn

  • Home
  • About
  • Publications
  • Departments
    • agile
    • architecture
    • cloud
    • java
    • mobile
    • open standards
    • oracle
    • oss
    • other
    • soa
    • software development
    • tablet
    • Uncategorized
    • web
  • Subscribe via RSS

OpenOffice Python API much nicer than Java API

February 18th, 2009  |  Published in java, open standards, oss  |  2 Comments

I wrote a small Python program today, to see if a could create a workaround for a problem i have in Java. I’ve written a couple of Java applications that use OpenOffice’s API to create ODF and MS-Word documents. The OpenOffice API that i used in Java is pretty horrible: very verbose, not intuitive. You can’t use intellisense/autocomplete, instead you query interfaces, that you just have to know exist. I documented some examples here: Getting started using openoffice in java, Mailmerge MS-Word template using OpenOffice and Java.

To my surprise, the python api doesn’t require you to use the whole query interface nonsense. Makes you code a lot smaller and more readable.

Here’s an example that replaces database field in a document (similar to the example here: Mailmerge MS-Word template using OpenOffice and Java).

#!/usr/bin/python

import sys
import uno
from os import getcwd
from unohelper import Base, systemPathToFileUrl, absolutize
from com.sun.star.beans import PropertyValue, UnknownPropertyException
from com.sun.star.uno import Exception as UnoException, RuntimeException

localContext = uno.getComponentContext()
resolver = localContext.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", localContext )
ctx = resolver.resolve( "uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext" )
smgr = ctx.ServiceManager
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx )

inProps = PropertyValue( "Hidden" , 0 , True, 0 ),
fileUrl = absolutize(systemPathToFileUrl( getcwd() ) ,"letter1.ott")
doc = desktop.loadComponentFromURL( fileUrl , "_blank", 0, inProps )

fields = doc.getTextFields().createEnumeration()
while fields.hasMoreElements():
        field = fields.nextElement()
        fieldname = field.getPresentation(0)

        try:
                isDatabaseField = field.getPropertyValue("DataBaseFormat")
        except UnknownPropertyException :
                isDatabaseField = False

        if isDatabaseField:
                try:
# this is incorrect usage of insertString
#                        doc.Text.insertString(field.getAnchor(),"New value for " + fieldname,1)
# this is the correct approach
                        fieldAnchor = field.getAnchor()
                        fieldAnchor.Text.insertString(fieldAnchor,"New value for " + fieldname,1)
                except RuntimeException, e:
                        print "RuntimeException while to replacing field: " + e.Message

# save document
outProps = PropertyValue( "Overwrite" , 0 , True, 0 ),
saveUrl = absolutize(systemPathToFileUrl( getcwd() ) ,"letter1.odt")
doc.storeToURL(saveUrl, outProps)

Beware of this code though, it throws a RuntimeException when trying to replace a database field in the header of a document. So, in that sense, Python is no better than Java.

Update: a reply on the openoffice forum put me on the right track. My usage of insertString was incorrect. I’ve fixed the code above.

  • Steen

    Actually I was looking for someting easy to script som oo documents. But I find this neither extremely readable and nor intuitive. Sorry

  • http://none Steen

    Actually I was looking for someting easy to script som oo documents. But I find this neither extremely readable and nor intuitive. Sorry

Tags

activemq agile bi camel css esb google governance grails groovy gtalk html5 internet ipad ivy J2EE java javascript jaxrs jmx jquery lean linkeddata linux maven mule noiv openoffice opensource Open Source oracle osgi oss rdbms rest scrum servicemix soa sql svg tablet web 2.0 XML xmpp yql

Archives

  • February 2012
  • January 2012
  • December 2011
  • November 2011
  • October 2011
  • September 2011
  • August 2011
  • July 2011
  • June 2011
  • May 2011
  • April 2011
  • March 2011
  • February 2011
  • January 2011
  • December 2010
  • November 2010
  • October 2010
  • September 2010
  • August 2010
  • June 2010
  • March 2010
  • February 2010
  • January 2010
  • December 2009
  • November 2009
  • October 2009
  • August 2009
  • July 2009
  • June 2009
  • May 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • February 2007
  • January 2007
  • December 2006
  • November 2006
  • October 2006
  • August 2006
  • July 2006
  • June 2006
  • May 2006
  • April 2006
  • March 2006
  • January 2006
  • December 2005
  • November 2005
  • October 2005
  • September 2005
  • August 2005
  • July 2005
  • June 2005

Meta

  • Log in
  • Entries RSS
  • Comments RSS
  • WordPress.org

Recent Posts

  • Self-service architecture to avoid water-scrum-fall
  • Updating a vagrant box
  • Using littleproxy in Mule unit tests
  • Useful site to determine what html5, css3 & svg you can use
  • A Product Owner is a Project Manager

Categories

  • agile
  • architecture
  • cloud
  • java
  • mobile
  • open standards
  • oracle
  • oss
  • other
  • soa
  • software development
  • tablet
  • Uncategorized
  • web

Recent Comments

  • From DevOps to NoOps to NoCollabs :: Andrej Koelewijn on LAC2011 presentation: Scrum under Architecture
  • andrej on Software architecture, PHP and Javascript
  • Pcdiggs on Software architecture, PHP and Javascript
  • Een Scrum Product Owner is een Project Leider on A Product Owner is a Project Manager
  • Using css webfonts in inkscape :: Andrej Koelewijn on Create presentations using inkscape
Buzz
andrkoel: Utrecht hele dag mist, scheveningen zomerse dag... http://t.co/cDRCJHr9
4:35 PM Nov 10, 2011, comment
andrkoel: RT @stefanvdkamp: Beter filmpje van Garret McNamara in de 30 meter hoge golf. http://t.co/9abiWkYX
9:20 AM Nov 10, 2011, comment
andrkoel: Mmm, een uur voor den haag - leiden lijkt te weinig, ga mijn afspraak niet redden...
7:34 AM Nov 10, 2011, comment
andrkoel: Big Data is Useless without Science http://t.co/2I8EiLsH
6:18 AM Nov 10, 2011, comment
andrkoel: Just tried #vagrant to quickly setup virtualbox development environment. Looks good, although provisioning apache through puppet failed...
10:47 PM Nov 09, 2011, comment
 


©2012 Andrej Koelewijn
Powered by WordPress using the Gridline Lite theme by Graph Paper Press.