lino.utils.odsreader

OdsReader uses odfpy to extract data from an .ods document (OpenOffice.org spreadsheet).

Thanks to Marco Conti and gtr for their blog post Read an ODS file with Python and Odfpy which was a valuable source of inspiration to me. Unlike Marco's reader this one doesn't store any data in memory, it just loops over the rows.

OdsReader is used to import data from .ods files into a Django database using a The Python serializer fixture, but not limited to this usage.

State : works for me, but very young and probably full of bugs.

Usage example:

The following code reads a file odsreader_sample.ods and prints a line of text for each row of data.

>>> class Sample(OdsReader):
...     filename = srcpath('odsreader_sample.ods')
...     headers = ["N°", "Prénom", "Last name", "Country", "City", "Language"]
...     column_names = 'no first_name last_name country city language'.split()
...    
>>> for row in Sample().rows():
...     print( "%(first_name)s %(last_name)s from %(city)s" % row)
Rudi Rutté from Eupen
Romain Raffault from Liège
Rando Roosi from Tallinn
Rik Rozenbos from Antwerpen
Robin Rood from London

(Note: these are fictive person names from lino.modlib.users.fixtures.demo).

(This module's source code is available here.)

Functions

srcpath(filename)

Classes

OdsReader(**kw)

Like SimpleOdsReader, but each row is converted to an lino.utils.AttrDict.

SimpleOdsReader(**kw)

Abstract base class.

class lino.utils.odsreader.SimpleOdsReader(**kw)

Bases: object

Abstract base class. For each .ods file you are probably creating a subclass of this.

filename = None

The full path name of the .ods file to be read.

headers = None

A list of unicode strings, one for each column in the file. The headers specified here must match exactly those found in the .ods file.

cells2row(cells)

This will be called for each recognized data row and may perform a conversion before yielding it. Subclasses may override this.

rows()

Yields the data rows found in this .ods file.

class lino.utils.odsreader.OdsReader(**kw)

Bases: lino.utils.odsreader.SimpleOdsReader

Like SimpleOdsReader, but each row is converted to an lino.utils.AttrDict. This requires you to specifiy, besides the SimpleOdsReader.headers attrribute, another list of pure ASCII strings which must be valid Python attribute names.