Andrej Koelewijn

  • Home
  • About
  • Departments
    • 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  |  1 Comment

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.

Share and Enjoy:
  • del.icio.us
  • Google Bookmarks
  • DZone
  • SphereIt
  • StumbleUpon
  • Technorati
  • LinkedIn
  • HackerNews
  • PDF
  • Digg
  • Facebook
  • FriendFeed
  • Posterous
  • Tumblr
  • Twitter
  • RSS

View Comments

Feed Trackback Address
  1. Steen says:

    May 30th, 2009 at 8:57 pm (#)

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

Leave a Response

blog comments powered by Disqus

Tags

bi bpel camel cep css dsl esb esper google governance grails groovy gtalk html5 innovation internet ipad ivy java javascript jaxrs jersey jigsaw jquery linkeddata linux maven middleware mule noiv openoffice openweb oracle osgi oss plsql rdbms rest soa sql sun tablet web 2.0 xmpp yql

Archives

  • 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

Meta

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

Recent Posts

  • Nice Java Decompiler tool: JD
  • VMware Player: The virtual machine is busy.
  • Adding a maven repository for installing features to ServiceMix
  • Upgrade Apache Camel in ServiceMix to version 2.3.0
  • A composite Rest service using Apache Camel

Categories

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

Recent Comments

  • Buddhika on Using google talk from java example
  • Anonymous on A composite Rest service using Apache Camel
  • Guest on How to find true cause of com.sun.star.uno.RuntimeException?
  • Absent Code attribute in method that is not native or abstract « Gooder Code - web development blog, php, java, asp.net, html, javascript on Absent Code attribute in method that is not native or abstract
  • Rmfume on Oracle best thing that could happen to JavaFX?
Buzz
andrkoel: RT @monkchips: James Governor's Monkchips » Day of The Dead: Web Drives Strong Demand for Java Skills http://monk.ly/d4UlND
9:17 PM Sep 03, 2010, comment
andrkoel: RT @monkchips: In which my business partner @sogrady explains Why You Should Pay Attention to Node.Js http://monk.ly/a4aGIP serverside # ...
3:06 PM Sep 03, 2010, comment
andrkoel: RT @stilkov: http://bit.ly/cDdqgl - AWS Identity and Access Management — I'd hate to have to compete against Amazon's Cloud offerings
8:47 AM Sep 03, 2010, comment
andrkoel: Twitter for ipad is nice, but i still think i need a tool to summarize all info, something like feedly or flipboard is the future
8:36 AM Sep 02, 2010, comment
andrkoel: Trying out the new twitter for ipad... Curious how the panels work.
8:32 AM Sep 02, 2010, comment
 


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