Here is some code for reading Excel spreadsheets (2003 and before) with PyExcelerator. My main problem is that if I leave a cell blank, it gives me an error. I want to be able to have blank cells on my spreadsheet without any problems. Does anyone have any ideas?
This is a spreadsheet with data on only the first 4 columns.
import pyExcelerator book = pyExcelerator.parse_xls("ReadFrom1.xls") parsed_dictionary = book readlist =  number_of_rows = len(parsed_dictionary)/4 #Divided by the number of columns to get number of rows for i in range(0,number_of_rows): readlist.append([parsed_dictionary[i,0],parsed_dictionary[i,1], parsed_dictionary[i,2],parsed_dictionary[i,3]]) #I have to add parsed_dictionary[i,(last number + 1)] for each additional column for i in readlist: print i
If my excel spreadsheet looks like
My output is:
[1.0, 2.0, 3.0, 4.0]
But when I take out the letter the number "2" in my excel sheet, my output becomes:
Traceback (most recent call last): File "C:/Python26/April_2010/Read With PyExcelerator.py", line 10, in <module> readlist.append([parsed_dictionary[i,0],parsed_dictionary[i,1], parsed_dictionary[i,2],parsed_dictionary[i,3]]) #I have to add parsed_dictionary[i,(last number + 1)] for each additional column KeyError: (0, 1)
Does anybody know how I can solve or work around this?
Also, as you can probably tell from my code, I have to manually add additional columns into the program and also account for that in the parsed_dictionary for loop every time I update my spreadsheet with additional columns.
Does anyone have any suggestions to handle this dynamically?
Thanks and I hope that some of you find this code useful.