Python and OpenOffice

Reading OpenOffice Calc sheets.

oocalcread is a module that enable you to read a Calc file as a big “matrix”. The file you need is, uncompress it in your current directory or in your python library path, and then import it. As an example, converting a oocalc file to a csv is:

import oocalcr
import csv

if len(sys.argv)==2:
    oobook = oocalcr.OOCalcRead(sys.argv[1])
    print >> sys.stderr, "Usage: %s <OO_calc_file>" % sys.argv[0]

writer=csv.writer(sys.stdout, quoting=csv.QUOTE_ALL)

for sheet in oobook:  # all sheets
    for l in sheet:
        a = ['%s' % i.encode("latin1","replace") for i in l]

Basically, OOCalcRead(file_name, trim=True, strip=False) return a list of sheets, every sheet is a list of rows, and every row is a list of cells (columns). All the values are unicode strings. Sheets and rows are python lists, with the difference that sheets have the attribute name which is the name of the sheet (notice that the order of the sheets can be different from ehat you see in your OOCalc). trim says to not to add rows completely empty, and to cut the cells at the last no-empty one of the row. strip strips blanks for each cell.

Attached files

Python Module to read OpenOffice Calc sheets.
Filename : (2 KB)
Caption : Python Module to read OpenOffice Calc sheets.

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>