[Tutor] Select rows and columns in excel
jfouhy@paradise.net.nz
jfouhy at paradise.net.nz
Thu Jul 28 00:55:10 CEST 2005
Quoting David Holland <davholla2002 at yahoo.co.uk>:
> Dear Tutors,
>
> I know how to open files in python, however what I want to do is select
> some information from an excel spreadsheet and save it as a .dat file.
> The bit, I am stuck on is :-
> How can I select all rows with a row number greater than x for a certain
> column ?
If you don't want to go with COM, there are a couple of packages that might be
useful: xlrd and pyExcelerator.
I've used pyExcelerator; it includes functions to parse a spreadsheet into a
dictionary. You could do something like this (untested):
import pyExcelerator
workBook = pyExcelerator.parse_xls('myfile.xls')
sheet = workBook['Sheet1'] # Or whatever the worksheet is called
print sheet.items()[:5]
# prints something like: ((3, 2), 'foo'), ((1, 0), 'bar'), etc
rowsAbove3 = dict([(x, sheet[x]) for x in sheet if x[0] > 3])
# etc
--
John.
More information about the Tutor
mailing list