Python and OpenOffice

Reading OpenOffice Calc sheets.

oocalcread is a module that enable you to read a OpenOffice.org Calc file as a big “matrix”. The file you need is oocalcr.zip, 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])
else:
    print >> sys.stderr, "Usage: %s <OO_calc_file>" % sys.argv[0]
    sys.exit(1)

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]
        writer.writerow(a)

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 : oocalcr.zip (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>

  

  

  

This site uses Akismet to reduce spam. Learn how your comment data is processed.