OOo spreadsheet to Excel with Python

UPDATE 2019: It seems that the website containing the library required by the below code is down. As such, it’s not really useful anymore.


A guy named Alf asked me on Usenet about saving OpenOffice.org spreadsheets to Excel files, using Python. After some more digging, this is what I came up with:

#!/usr/bin/env python

'''Demonstration program for saving an OpenOffice.org spreadsheet as
Excel sheet. Sometimes people refuse to install OOo, even though it's
completely free...

Requires that OOo is started with:
    ooffice "-accept=socket,host=localhost,port=8100;urp;"

Then open the spreadsheet as usual, and run this Python script.

Also requires Danny's OOo lib from
    https://www.oooforum.org/forum/viewtopic.php?p=56015
'''

import Danny.OOo.OOoLib as OOoLib
import unohelper

# For PDF output change the filter to:
# OOoLib.makePropertyValue('FilterName', 'writer_pdf_Export'),

properties = (
    OOoLib.makePropertyValue('FilterName', 'MS Excel 97', -1, 0),
)

filename = '/tmp/test.xls'

path = unohelper.systemPathToFileUrl(filename)
desktop = OOoLib.getDesktop()
doc = desktop.getCurrentComponent()
doc.storeToURL(path, properties)

As with the other script, this one requires Danny’s OOo lib.

dr. Sybren A. Stüvel
dr. Sybren A. Stüvel
Open Source software developer, photographer, drummer, and electronics tinkerer

Related