Python (pyuno) "Hello World" Addon for OpenOffice
In my last few posts about pyuno (SSConverter, OORunner) we used pyuno to convert spreadsheets to CSV files by running OpenOffice from Python using pyuno as the bridge between the two processes. In this post we're going to get inside OpenOffice and use pyuno as the bridge between OpenOffice and an embedded Python interpreter (embedded inside OpenOffice).
As is the law, our first program when doing something new has to be a "Hello World" program. The idea here is to add an option to the OpenOffice menu which will insert "Hello World" into the first cell of a spreadsheet. As a starting point for this example I used an example from the pyuno udk site (the second "Hello World" example on the page).
The example there does essentially the same thing as the example here except there it inserts "Hello World" into a Writer (Word Processor) document rather than a spreadsheet. One would assume that it would be a fairly easy conversion, and once I knew the answer it was. The hard part was figuring out how to get the "spreadsheet" object and then how to get a "cell" object from it. And it wasn't so much hard as just time consuming trying to find the right information in the documentation.
After a few hours of searching and a few false starts I came across the two needed interfaces: XSpreadsheetDocument and XCell. These gave me what I needed to modify the example to work with a spreadsheet:
1 import uno 2 import unohelper 3 4 from com.sun.star.task import XJobExecutor 5 6 # Implement an UNO component by deriving from the standard 7 # unohelper.Base class and from the interface(s) you want to implement. 8 class HelloWorldJob(unohelper.Base, XJobExecutor): 9 def __init__(self, ctx): 10 # store the component context for later use 11 self.ctx = ctx 12 13 def trigger(self, args): 14 # Retrieve the desktop object 15 desktop = self.ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", self.ctx) 16 17 # Get the spreadsheet. 18 spreadsheet = desktop.getCurrentComponent() 19 20 # Get the collection of sheets in the spreadsheet. 21 sheets = spreadsheet.getSheets() 22 23 # Get the first cell in the first sheet. 24 cell = sheets.getByIndex(0).getCellByPosition(0, 0) 25 26 # Modify its contents. 27 if cell.getFormula(): 28 cell.setFormula("Hello " + cell.getFormula()) 29 else: 30 cell.setFormula("Hello world") 31 32 33 # pythonloader looks for a static g_ImplementationHelper variable 34 g_ImplementationHelper = unohelper.ImplementationHelper() 35 36 g_ImplementationHelper.addImplementation( \ 37 HelloWorldJob, # UNO object class 38 "org.openoffice.comp.pyuno.demo.HelloWorld", # Implementation name 39 ("com.sun.star.task.Job",),) # List of implemented services
The meat of the example is the trigger method of the HelloWorldJob class. This method is called when the add-on is invoked. Trigger performs the following steps:
- It gets the top-level desktop object.
- From the desktop object it gets the current component. The current component in this case is a SpreadsheetDocument.
- From the spreadsheet object it gets the collection of all the sheets in the spreadsheet.
- From the sheets collection it gets the first sheet and then the first Cell from that sheet.
- Using the cell, if the cell is empty it inserts "Hello World". If the sheet contains something then it prefixes the old contents with "Hello ".
The rest of the code is essentially a copy of the code from the original example.
Now that we have the code we need to integrate it into OpenOffice. We do that by creating a zip file of the code and a XML file that describes the add-on. The XML file is, again, essentially just a copy from the original example:
<?xml version="1.0" encoding="UTF-8"?> <oor:node xmlns:oor="http://openoffice.org/2001/registry" xmlns:xs="http://www.w3.org/2001/XMLSchema" oor:name="Addons" oor:package="org.openoffice.Office"> <node oor:name="AddonUI"> <node oor:name="AddonMenu"> <node oor:name="org.openoffice.comp.pyuno.demo.HelloWorld" oor:op="replace"> <prop oor:name="URL" oor:type="xs:string"> <value>service:org.openoffice.comp.pyuno.demo.HelloWorld?insert</value> </prop> <prop oor:name="ImageIdentifier" oor:type="xs:string"> <value>private:image/3216</value> </prop> <prop oor:name="Title" oor:type="xs:string"> <value xml:lang="en-US">Insert Hello World</value> </prop> </node> </node> </node> </oor:node>
In addition to creating the zip file containing these two files you need to run unopkg to register the add-on with OpenOffice. The following bash script does the zipping and the packaging and then runs OpenOffice so that you can test it:
#!/bin/bash unopkg_bin=/usr/bin/unopkg oocalc_bin=/usr/bin/oocalc addons=Addons.xcu python_file=hello_world_oocalc.py zip_file=hello_world.zip rm $zip_file zip $zip_file $addons $python_file $unopkg_bin remove $zip_file $unopkg_bin add $zip_file #export PYUNO_LOGLEVEL=CALL export PYUNO_LOGLEVEL=ARGS export PYUNO_LOGTARGET=stdout $oocalc_bin
When you run the script it will start OpenOffice and you should see the following option in the "Tools" menu:
If you select the option the add-on should modify the spreadsheet and you should now have:
If you select the option again you should get:
Mitch Frazier is an Associate Editor for Linux Journal.
Webinar: 8 Signs You’re Beyond Cron
11am CDT, April 29th
Join Linux Journal and Pat Cameron, Director of Automation Technology at HelpSystems, as they discuss the eight primary advantages of moving beyond cron job scheduling. In this webinar, you’ll learn about integrating cron with an enterprise scheduler.Join us!
|Android Candy: Intercoms||Apr 23, 2015|
|"No Reboot" Kernel Patching - And Why You Should Care||Apr 22, 2015|
|Return of the Mac||Apr 20, 2015|
|DevOps: Better Than the Sum of Its Parts||Apr 20, 2015|
|Play for Me, Jarvis||Apr 16, 2015|
|Drupageddon: SQL Injection, Database Abstraction and Hundreds of Thousands of Web Sites||Apr 15, 2015|
- Tips for Optimizing Linux Memory Usage
- "No Reboot" Kernel Patching - And Why You Should Care
- DevOps: Better Than the Sum of Its Parts
- Return of the Mac
- Android Candy: Intercoms
- Drupageddon: SQL Injection, Database Abstraction and Hundreds of Thousands of Web Sites
- Designing Foils with XFLR5
- Non-Linux FOSS: .NET?
- Play for Me, Jarvis