Writing an OpenOffice.org Calc extension in Python

December 13, 2009 at 10:08 AM | categories: python, openoffice.org | View Comments

A very well organized group of thugs steals my money every day. I'm getting really tired of it, but I found a way to save at least some of my money so they don't steal nearly as much of it, nearly as often. For whatever reason, this gang hasn't devised a way to steal gold and silver nearly as efficiently as these little green pieces of paper that they call money. So I buy gold and silver whenever I can as a hedge against 'inflation' (which is really just their gang-speak for the wholesale-theft of my money.)

I want to keep track of how much gold and silver I've bought, when I bought it, and inevitably I'll still want to know how many green pieces of paper I could theoretically trade it all in for.

The easiest way to track all this information was to create a spreadsheet in OpenOffice.org's Calc. I created a sheet for gold purchases and another sheet for silver purchases. Everytime I make a purchase, I record the number of ounces, what form it is in (coins, bars, junk etc), who I bought it from, when I bought it, and the price I paid in FRNs (Federal Reserve Notes, aka green pieces of paper.) On another sheet I total the number of ounces I own and multiply it by the current spot price for the metal, which gives me the current price I could get if I sold it for FRNs. When tallying this total, I reference a special cell on the sheet, one that has the current spot price for gold, and another for silver.

But checking the spot price myself and manually updating those cells was just too tedious for my programmer's heart. I wanted OpenOffice.org to automatically retrieve this information for me. So, I started researching OpenOffice.org extensions (plugins).

Turns out, OpenOffice.org extensions can be written in my favorite programming language, Python (yea!), so I wrote the following extension a few weekends ago. It's on github and can be downloaded with git:

  git clone git://github.com/EnigmaCurry/SpotMetal.git

If you just want the pre-compiled extension, it can be downloaded here locally:


You install it inside OpenOffice.org by going to Tools -> Extension Manager and clicking on Add and browsing to the SpotMetal-0.1.oxt file you downloaded or built yourself.

Once you have it installed, you now have a new Calc function available called SPOTMETAL which takes two arguments:

  • metal - Which metal you want to look up. Can be one of "gold", "silver", "platinum", or "palladium"
  • bidAsk - Whether you want the bid or the ask price. Can be either "bid" or "ask".

The price will automatically be refreshed every 5 minutes by default, but you can also force a refresh with the Calc function called SPOTMETALREFRESH.

Here's an example OpenOffice.org spreadhseet that shows how you might track your own precious metal investment portfolio. The big button labeled "Refresh Spot Price" does what it says it does, but requires a bit more boiler-plate code in order for it to actually display on screen. In OpenOffice.org, you can see another macro called doReCalculate:

  • Tools->Macros->Organize Macros->OpenOffice.org Basic
  • MetalTrackerExample.ods->Standard->SpotMetal->doReCalculate.

This extra macro is necessary to actually force the spreadsheet to request new data from the extension when you click the "Refresh Spot Price" button.

While this extension might be of use to you if you also invest in precious metals, I guess the main reason I posted this is because it took a good deal of time researching how to write a Python extension for OpenOffice.org. Check out the source code on github if you're looking to write your own extension, it's got it's own Makefile and hopefully it's documented well enough for it be useful for someone in the future.

Read and Post Comments