Small hack on Python and OpenOffice.org

So I want to make a small tutorial for a baby step world. The main point is to re-use the python samples included in OpenOffice.org. We won’t generate code, instead we will just compare notice the difference and modify slightly to change it’s behavior.

The first thing we will do is copy the samples that are on the OpenOffice.org system and move them to our OpenOffice.org profile. We will assure that the permissions are assigned to us and finally try to make the pythonTables script behave like the HelloWorld script.

The HelloWorld insert a text into the document, while the pythonTables will generate a new document and then insert text, tables, frames and other writer utilities.

Let’s get going!!!

First we will copy our script:

$ sudo cp -Rv /opt/openoffice.org2.2/share/Scripts/python/ \   ~/.openoffice.org2/user/Scripts/

Now we will change the permissions of the script:

$ cd ~/.openoffice.org2/user/Scripts/ \$ chown -Rv user.user python/

where user is your current user account name.

Now we will have some writing permissions:

$ chmod -Rv 766 python/*

Let the Hacking begin!!

So first we will run the scripts just to see that they work and nothing has gone wrong….

  • First step is starting OpenOffice.org: Launch OpenOffice.org
  • Go to Tools > Macros > Organize macros > Python…
  • You will face a dialog with a list of Python macros divided into 3 areas My Macros, OpenOffice.org Macros and Untitled.
  • Go to My Macros and expand it by clicking on the “+”
  • Select the HelloWorld and HelloWorldPython and click on Execute.

This will insert the text Hello World (in Python) on the current document.

Now lets try the other script, TableSample which will add a full text of the document. Repeat the steps that we mentioned before but instead of selecting HelloWorld, go to PythonSamples > TableSample > createTable.

This will generate a New Document with a bunch of information. So we now know that our scripts are working properly.

Open a text editor and open HelloWorld.py and TableSample.py from the ~/.openoffice.org2/user/Scripts/python folder. TableSamples might be in the pythonSamples folders so you will have to dig deeper to find it.

You will see that HelloWorld is a simple script of just 13 lines of code while TableSample.py have nearly 96 lines of code. Don’t let that intimidate you, most of the lines are just because of the amount of data that it dump to the document, but the principle is not that many lines different.

Look at the HelloWorld.py and you will see the following line:

model = XSCRIPTCONTEXT.getDocument()#get the XText interface    text = model.Text

This is the most important line and the key to what we want. We can see that there is something called the XSCRIPTCONTEXT. This is what we are looking for and major difference between both codes.

XSCRIPTCONTEXT will save us a lot of hacking in the future of coding with pyuno. However now this line will help us use the current document.

Now lets turn to the TableSample.py, we will see many functions each one with a good chunk of code. If we look closer we can see that we have 2 functions, insertTextIntoCell, and createTable. InsertTextintoCell is just a few lines long and we can see that there is nothing to our use since it focus on configuring the table. We look into the createTable and we see immediately the comment: creates a new writer document and inserts a table with some data (also known as the SWriter sample).

See that wasn’t that hard, we see that following the comment there is a line like:

ctx=uno.getComponent()

We remember that the first script had something similar but on XSCRIPTCONTEXT instead of uno and getDocument() instead of getComponent(). If we keep on looking at the code we see other lines:

smgr = ctx.ServiceManager    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)        # open a writer document    doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )

This becomes clearer that if something in the code should change, should be this part. The doc variable is the one that actually generates the new writer document. And then we will see actions applied to the doc container like on line 32 when we generate a table by creating an instance of the TextTable.

table = doc.createInstance( "com.sun.star.text.TextTable" )

If we go back to HelloWorld.py we have the model variable as opposed to the doc variable but there is no fundamental difference and we can just replace that chunk of code. So we will just comment those lines and have something like:

def createTable():    """creates a new writer document and inserts a table with some data (also known as the SWriter sample)""" #    ctx = uno.getComponentContext()#    smgr = ctx.ServiceManager#    desktop = smgr.createInstanceWithContext( "com.sun.star.frame.Desktop",ctx)        # open a writer document#    doc = desktop.loadComponentFromURL( "private:factory/swriter","_blank", 0, () )    model = XSCRIPTCONTEXT.getDocument()    text = model.Text    cursor = text.createTextCursor()    text.insertString( cursor, "The first line in the newly created text document.n", 0 )    text.insertString( cursor, "Now we are in the second linen" , 0 )        # create a text table    table = model.createInstance( "com.sun.star.text.TextTable" )

However since we mention before that the original code reference the document as ‘doc’ and now we just assigned to ‘model’ we can do a search and replace so that the instructions like the table one reference to model.

We go ahead and execute createTable under Tools > Macros > Organize Macros > Python… and vioala we are done.

Media_httpfilesmyoper_ejhbc

Final shot
Happy hacking!!!

Advertisements