Wednesday, September 22, 2010

Python, pyuno and OpenOffice

Some time ago, I discovered Python. Then I saw that he can work with the OpenOffice.
Today I will briefly introduce how to do this. I do this in Windows XP, and Windows users could benefit.
In the folder : C:\Program Files\ 3\program there was a file called python.exe.
Go there and type the following script to start OpenOffice:

import os
import subprocess
import sys
import time
import uno

NoConnectionException = uno.getClass("")
ooffice = 'soffice "-accept=socket,host=localhost,port=8100;urp;"'
def start_OOo():
    '''Starts with a listening socket.'''
    # Start and report any errors that
    # occur.
        retcode =, shell=True)
        if retcode < 0:
            print >>sys.stderr, \
                "OOo was terminated by signal", \
        elif retcode > 0:
            print >>sys.stderr, \
                "OOo returned", \
    except OSError, e:
        print >>sys.stderr, "Execution failed:", e
    # Terminate this process when OOo has closed.
    raise SystemExit()
started_OOo = False
if not started_OOo:
    print "Starting OOo"
    started_OOo = True
print "OOo started"
This will start OpenOffice 3.
We can use now python scripts to create any type of file used by OpenOffice.
Let show one image create with few lines :