In the May issue, we discussed programming using Open Office components. This time, we will take a database application using the simplest of databases and storing somewhat complex data in it. We will then extract the desired data and use the spreadsheet to offer a very nice user interface, which we have not had to write.
Many of us in the IT field have one stock which we wish to monitor. So, this is a good example for us to consider. We will normally buy a number of shares at a particular price. Occasionally, we will notice the price of the share and add it to the list of prices for this stock. However, for our viewing, we are interested in the purchase price, the range of prices and the current price.
We will use gdbm or equivalent simple database. These databases store a key and a value, both of which must be strings. Hence, the first part will demonstrate how we store objects easily in such a database. By the way, zodb is an excellent option if a person is interested in using an object database which is very easy to use as well.
So, let us start with our database part of the application. In order to focus on the essentials, the program is poorly written. It leaves out error handling and recovery completely and does not offer the user any flexibility.
The key concept to notice is that our stockdata is an array consisting of a variable number of items and not all of the same type. Hence, we can store fairly complex structures even in as simple a database as gdbm or Berkeley DB and manipulate it easily. Also, the syntax for using this database is identical to using a dictionary.
Now, our data is safe. We can give the desired data to the user which he can manipulate and process the way he needs. The test below was done with OpenOffice 2 Beta version but works with the stable 1.1 version as well. We will need to use the python interpreter, “programs/python”, in the open office installation directory. For OO-1.1 version, we need to use “import gdbm as dbm” instead of “import dbm” because the Python version is 2.2.
The first thing we need to do is start OpenOffice so that it is listening on a socket.
We then connect to it and get the desktop object.
It is worth highlighting at this point that Python code is much easier than the Java examples because Python does not require calls to queryInterface before calling a method in an interface supported by the object.
We need to create a new spreadsheet document and open a new spreadsheet.
We will now select the data for each stock from our database and load it in the spreadsheet.
We can add a little colour to our spreadsheet and colour-code the stocks based on current performance.
Now, we come to our utility function, setCellValue. It seems un-Pythonic to call a different method depending upon the type of data. Hence, we hide that need in a utility function as follows:
Needless to add, the utility function should appear in the source file before it is used.
Hopefully, with the previous article and this one, the pattern of programming OpenOffice becomes clearer. Formatting of the document is controlled by setting property values. The interfaces are inherited and used across similar objects. So, method names are common. After we get over the initial speed breakers, OpenOffice no longer appears so difficult to control through a program. It is fun.
Other Articles >