Skip to main content

Using PyODConverter with non-default ports

· 2 min read

I've been using the excellent PyODConverter (aka Python OpenDocument Converter) for quite some time now. It's a very small Python script that uses OpenOffice's Python-UNO bridge to convert documents between formats. For example, to convert test.doc to a PDF file, you could execute:

python DocumentConverter.py test.doc test.pdf
tip

For the Python-UNO bridge (and thus PyODConverter) to work, you do need to use the python binary that installs with OpenOffice, so you will most likely need to full-qualify the python command like:

/opt/openoffice.org3/program/python DocumentConverter.py test.doc test.pdf

Anyway, PyODConverter always tries to talk to OpenOffice via (the somewhat arbitrary) port 8100. But today I found myself needing to able to tell PyODConverter which port to use via the command line. So, I went about modifying the PyODConverter code - which was very easy, thanks to it being already very well written.

However, since PyODConverter is officially released under the GNU LGPL, my understanding is that I am now obliged to publish back the changes... sounds like a great excuse for another blog post ;)

So, here's the diff output for my (very minor) modifications to PyODConverter's DocumentConverter.py v1.0.0 script:

133a134
> port = DEFAULT_OPENOFFICE_PORT
135c136
< print "USAGE: python %s <input-file> <output-file>" % argv[0]
---
> print "USAGE: python %s <input-file> <output-file> [port]" % argv[0]
136a138,139
> elif len(argv) > 3:
> port = argv[3]
142c145
< converter = DocumentConverter()
---
> converter = DocumentConverter(port)

As you can see, the changes are quite simple and few. They enable the port number as an optional third command line parameter. For example:

python DocumentConverter.py test.doc test.pdf 12345

In that example, PyODConverter will try to use port 12345 to talk to OpenOffice. Of course, if you omit the optional port parameter, PyODConverter will simply default back to the original port 8100.