Skip to main content

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


Popular posts from this blog

Flowing text in inkscape (Poster making)

You can flow text into arbitrary shapes in inkscape. (From a hint here).

You simply create a text box, type your text into it, create a frame with some drawing tool, select both the text box and the frame (click and shift) and then go to text->flow into frame.


The omnipresent anonymous asked:
Trying to enter sentence so that text forms the number three...any ideas?
The solution:
Type '3' using the text toolConvert to path using object->pathSize as necessaryRemove fillUngroupType in actual text in new text boxSelect the text and the '3' pathFlow the text

Pandas panel = collection of tables/data frames aligned by index and column

Pandas panel provides a nice way to collect related data frames together while maintaining correspondence between the index and column values:

import pandas as pd, pylab #Full dimensions of a slice of our panel index = ['1','2','3','4'] #major_index columns = ['a','b','c'] #minor_index df = pd.DataFrame(pylab.randn(4,3),columns=columns,index=index) #A full slice of the panel df2 = pd.DataFrame(pylab.randn(3,2),columns=['a','c'],index=['1','3','4']) #A partial slice df3 = pd.DataFrame(pylab.randn(2,2),columns=['a','b'],index=['2','4']) #Another partial slice df4 = pd.DataFrame(pylab.randn(2,2),columns=['d','e'],index=['5','6']) #Partial slice with a new column and index pn = pd.Panel({'A': df}) pn['B'] = df2 pn['C'] = df3 pn['D'] = df4 for key in pn.items: print pn[key] -> output …

Drawing circles using matplotlib

Use the pylab.Circle command

import pylab #Imports matplotlib and a host of other useful modules cir1 = pylab.Circle((0,0), radius=0.75, fc='y') #Creates a patch that looks like a circle (fc= face color) cir2 = pylab.Circle((.5,.5), radius=0.25, alpha =.2, fc='b') #Repeat (alpha=.2 means make it very translucent) ax = pylab.axes(aspect=1) #Creates empty axes (aspect=1 means scale things so that circles look like circles) ax.add_patch(cir1) #Grab the current axes, add the patch to it ax.add_patch(cir2) #Repeat