Using Python with OpenOffice.org - Part 1/3

I wanted to write a modified version of the envelope creation macro described here, thinking that I would use Python to learn the OpenOffice.org API. OpenOffice.org developer documentation is horrible. Written by and for architecture astronauts, it is also not very complete for Python.

Background Reading

These are some useful places to start your education:

Debugging

The talk page tells you where to edit the file to get debugging on Linux. You have to restart OpenOffice to see the logging in the file, which is found by:

find ~/.openoffice* -name log.txt

So far I have found this log file to be utterly useless, even when on DEBUG level. It seems that output does not go there, but if you do not catch exceptions then they come up in OpenOffice dialogues anyway, which is more useful.

So the first task was to be able to do simple error handling and debugging. Initially printf into the document may be useful:

model = XSCRIPTCONTEXT.getDocument()
text = model.Text
cursor = text.createTextCursor()
text.insertString(cursor, "START", 0)
# do stuff
text.insertString(cursor, "END", 0)

Message Boxes

After reading for hours and pasting together from various documentation sources, this seems like a reasonable, modern message box function:

import uno
import traceback
from com.sun.star.awt.MessageBoxButtons import (BUTTONS_OK,
                                                BUTTONS_OK_CANCEL,
                                                BUTTONS_YES_NO,
                                                BUTTONS_YES_NO_CANCEL,
                                                BUTTONS_RETRY_CANCEL,
                                                BUTTONS_ABORT_IGNORE_RETRY,
                                                DEFAULT_BUTTON_OK,
                                                DEFAULT_BUTTON_CANCEL,
                                                DEFAULT_BUTTON_RETRY,
                                                DEFAULT_BUTTON_YES,
                                                DEFAULT_BUTTON_NO,
                                                DEFAULT_BUTTON_IGNORE)

def messageBox(message="", title="", msgtype="messbox", buttons=BUTTONS_OK, parentWin=None, rect=None):
    model = XSCRIPTCONTEXT.getDocument()
    context = XSCRIPTCONTEXT.getComponentContext()
    toolkit = context.ServiceManager.createInstanceWithContext("com.sun.star.awt.Toolkit", context)

    if not parentWin:
        parentWin = model.CurrentController.Frame.ContainerWindow

    if not rect:
        rect = uno.createUnoStruct('com.sun.star.awt.Rectangle') 

    msgtypes = ("messbox", "infobox", "errorbox", "warningbox", "querybox")
    msgtype = msgtype.lower()
    if msgtype not in msgtypes:
        msgtype = "messbox"

    msgbox = toolkit.createMessageBox(parentWin, rect, msgtype, buttons, title, message)

    if msgbox:
        result = msgbox.execute()
        return result
    else:
        raise Exception("Unable to create message box")

Phew. Another option is to make a class, although I don’t like the constant-hacking done in that example. Some key gotchas are that import uno must come first, you cannot import constants directly (or using *), and you must supply a valid parent window.

So now on to getting the address(es) from the document…

Installing a Dell 1320c Colour Laser Printer on Ubuntu (Lucid 10.04/Maverick 10.10)

Easy walk-through and pretty pictures courtesy of Technology Insight. If the link goes down, a new driver appears, or you don’t trust the package then you can create your own package using the official RPM from Fuji Xerox.

Convert the package

sudo aptitude install -y alien
wget ftp://ftp.fxa.com.au/drivers/dpc525a/dpc525a_linux_.0.0.tar.zip
unzip dpc525a_linux_.0.0.tar.zip
cd C525A_LinuxE/
dpkg-architecture -ai386 -c "fakeroot alien --scripts *.rpm" # debhelper changed to not work with other architectures

Find architecture

uname -m

32-bit install

sudo dpkg -i *.deb

64-bit install

UPDATE

Due to bug 831768 it’s highly recommended not to use aptitude on Ubuntu Oneric 11.10. Changed to apt-get below. More recent versions should be fine with aptitude.

sudo apt-get install -y ia32-libs
sudo dpkg -i --force-architecture *.deb

Setup printer

Now follow the usual method of installing a printer (walkthrough at the link above); the driver has manufacturer FX and then model DocuPrint C525 A-AP. Remember to set the paper source both when installing and set the default paper source in the printer properties afterwards, otherwise it will never feed sheets in.

Where is my printer?

If you need to figure out where your printer is, you can do something like:

sudo apt-get install -y nmap
nmap 192.168.0.0/22 10.0.0.0/22

(or fill in the correct networks to scan if you know them, will be much faster). Then find the IP address corresponding to something like this:

Nmap scan report for 10.0.1.11
Host is up (0.0061s latency).
Not shown: 996 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
80/tcp   open  http
515/tcp  open  priner
9100/tcp open  jetdirect

That is the address of the printer, which can then be used in Ubuntu’s New Printer dialogue.

Links

Fuji Driver Downloads
Another short guide
Using proprietary i386 apps on an amd64 system
Bug 730236 Bug 831768




Making ice cream part 2

This is after 1 hour in the Cuisinart. Consistency is almost the same and temperature not particularly cold (but not room temperature either). Note that after a few more hours in the freezer it tastes great and is pretty (well, very; too hard to scoop) solid and mostly ice-crystal-free. Is this the only function of the machine (reduce crystals - hardly perfect or worth the effort based on this evidence) and the advertised 45-60 minutes is just marketing? Or is there a fault? Other people seem to get edible/much thicker results after using it.