Managing Python Extensions in OpenOffice.org I

Media_httpfilesmyoper_iizhc

For all Python fans out there, you might not know that OpenOffice.org can be run on Python. This is great because OpenOffice.org is a very interesting application but is hard to modify or develop on top of it. However extensions has been the way to go when you want to implement something new in OpenOffice.org. So here is a mini tour oriented to the way OpenOffice.org manage Python, which is different to the way OpenOffice.org handles the native OOoBasic. One thing is that OOoBasic is compatible with OpenOffice.org IDE that you get when you go to Tools > Macros > Manage Macros > OpenOffice.org Basic.

How Macros are handled.
Macros are usually handled on 3 stages, it can be either file based, user based, or application base.

  • The file based it means that the macros are stored in the file, this files will have the python source code within the document.
  • User based is that the macros are saved on the user preferences, this is usually stored in linux on the dot folders, in windows under the Application data folder.
  • Application base are stored in the applications folder which in linux is under /opt folder and on windows is under the Program Files folder.

The difference between this thre methods are the accesibility that you will get, for example document macros gain the portability of sending OpenDocument files and transporting the macro to other people on different computers.

The user-centric affect just the individual user account and might not be transferable to other users, for that you will want the application base. This also gain the availability since you can use the macro regardless of the document you are working on and it lives in OpenOffice.org. The global one lives in OOo and affect all users, this is the best one if you want to make large deployments.

What about Python
As I said before, Python is not a native language to OpenOffice.org, however you will be happy to know that python is included by default under OpenOffice.org. Is stored at openoffice.org2.2/program/python-core-2.3.4/ (versions might change); so you are able to use this macros on every openoffice.org installation.

As I mention before, the 3 layers that OOo can store macros will affect the installation of new macros developed in Python.

The easiest one is to have python saved in your application, so let say that you have the file MyScript.py. To load it to your OpenOffice.org application will be:

$ su$ cp MyScript.py /opt/openoffice.org2.2/share/Scripts/ptyhon/

.

The user account level will be similar nad will execute by just doing the following:

$ cp MyScript ~/.openoffice.org2.2/user/Scripts/python/

* Be careful since the python folder is not created by default you will need to do create a folder under the Script folder.

The last one — the document level is actually quite hard and might just put it here for reference but is more complicated than just coping the script inside the document. You will actually have to edit an XML file in order to manually register the file inside the document.

As you may know OpenDocuments are Zip files containing other files. They also have a file structure which will include:

meta.xmlcontent.xmlstyle.xmlmimetypecurrent.xmlConfiguration2/   accelerator   images   popmenu   ...META-INF/   manifest.xmlThumbnails/   file.png

You will actually need to unzip and create a new folder called Scripts and inside create the python folder. Only then you will be able to copy your script MyScript.py inside the python folder.

Here is where you will need to edit the registry which is in the META-INF/manifest file and add 3 lines (before the final tag manifest:manifest].

<manifest:file-entry manifest:media-type="" manifest:full-path="Scripts/python/MsgBox.py"/> <manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/python/"/> <manifest:file-entry manifest:media-type="application/binary" manifest:full-path="Scripts/"/>

This will register the path of the script “Scripts/python/MsgBox.py” , the path of the scripting folders one for “Scripts/python” and other just for the “Scripts” folder.

Next post I will put some sample script so you can run and test your scripts meanwhile I will like to point to some default Python scripts already in OpenOffice.org.

Please go to Tools > Macros > Administer Macros > Python select the main application labeled as OpenOffice.org Macros and expand. You will see many sample codes that might look familiar to the ones you found at openoffice.org2.2/share/Scripts/python/HelloWorld.py you might also find Capitalized and the folder pythonSamples.

Later I will suggest you some useful sites to download some Python components.

Advertisements