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.
|Designing Electronics with Linux||May 22, 2013|
|Dynamic DNS—an Object Lesson in Problem Solving||May 21, 2013|
|Using Salt Stack and Vagrant for Drupal Development||May 20, 2013|
|Making Linux and Android Get Along (It's Not as Hard as It Sounds)||May 16, 2013|
|Drupal Is a Framework: Why Everyone Needs to Understand This||May 15, 2013|
|Home, My Backup Data Center||May 13, 2013|
- RSS Feeds
- Dynamic DNS—an Object Lesson in Problem Solving
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- New Products
- Using Salt Stack and Vagrant for Drupal Development
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Validate an E-Mail Address with PHP, the Right Way
- What's the tweeting protocol?
- Tech Tip: Really Simple HTTP Server with Python
- Kernel Problem
1 hour 11 min ago
- BASH script to log IPs on public web server
5 hours 38 min ago
9 hours 14 min ago
- Reply to comment | Linux Journal
9 hours 46 min ago
- All the articles you talked
12 hours 10 min ago
- All the articles you talked
12 hours 13 min ago
- All the articles you talked
12 hours 14 min ago
16 hours 39 min ago
- Keeping track of IP address
18 hours 30 min ago
- Roll your own dynamic dns
23 hours 43 min ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?