Saturday, March 16, 2013

Reading spreadsheet data in Python: The lack of a good ODS reader

I try and keep long term data in as simple a format as possible, which means text where ever possible.

In earlier times I would enter data in excel spreadsheets and then read them from my Python programs using the xlrd package which is excellent. This works well, but in the back of my mind is the thought that someday Microsoft might do something funny with their business model making office software more janky to use and all my fears about keeping data in proprietary formats would come true. Oh, look, that day is today.

So, I'm completely abandoning the MS Office suite and going back to basic text files.

However, there is a tension between keeping tabulated data in a simple form, such as csv, and entering it in a convenient manner.

Excel, of course, nags you everytime you edit a csv file and save it. Libreoffice is excellent: it handles loading and saving in a very streamlined fashion. However, every time you open up the csv file you need to tell Calc what widths you want for the cells and how to wrap the cells (for textual data) till you get it how you like it so its comfortable to enter data.

The .ods format, because it is a standard, stands some chance of being read in the future if it is widely adopted, but really nothing beats text.

So, my compromise is to tabulate the data using LO and then, at the end, archive it as a csv file.

What I would really like is a python package that mirrors the abilities of xlrd and allows me to read my .ods file from Python. I could export to csv every time and use , but I will forget to do that once in a while, and it will usually be at an inopportune time and it will take me hours to chase down why I can't see the updated data. So, I would rather read the data directly from the .ods file.

The solutions for that, while not as polished as xlrd, do exist:
  1. odfpy - not really directly usable, more like an API
  2. simple-odspy - decently user friendly, uses odfpy
  3. odsreader - very easy to use, but loads whole spreadsheet into memory at a time.
UPDATE: I'm finding the use of odfpy and other modules slightly buggy for my spreadsheets. I've reverted to using Libre Office to generate .xlsx and using the tried and trusted xlrd module to read the files

No comments:

Post a Comment