2

What I need to know is, can I get Python to read a spreadsheet (preferably Microsoft Excel), then parse the information and input it into an equation?

It's for a horse-racing program, where the information for several horses will be in one excel spreadsheet, in different rows or columns. I need to know if I can run a calculation for each of those horses separately and then calculate a score for the given horse.

reducing activity
  • 1,985
  • 2
  • 36
  • 64
vess
  • 119
  • 2
  • 3
  • 8

4 Answers4

3

My suggestion is:

  1. Save the Excel file as a csv comma separated value file, which is a plain text format and much easier to work with.
  2. Use Python's built-in csv module to work with the data in csv format.

You can work with Excel files directly in Python (Excel 2003 format supported via the third party modules xlwt, xlrd) but this is much harder than working with CSV.

Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
3

OpenPyXL ("A Python library to read/write Excel 2007 xlsx/xlsm files") has a very nice and Pythonic API.

Walter
  • 7,809
  • 1
  • 30
  • 30
1

Use xlrd package. It's on PyPI, so you can just easy_install xlrd

vartec
  • 131,205
  • 36
  • 218
  • 244
0

You can export the spreadsheet as a .csv and read it in as a text file, then process it. I have a niggling feeling there might even a CSV parsing python library.

AFAIK there isn't a .xls parser, although I might be wrong.

EDIT: I was wrong: http://www.python-excel.org/

n00dle
  • 5,949
  • 2
  • 35
  • 48
  • The `csv` module is built into Python. And there's no Excel parser in vanilla Python, though there are third party modules. `xlrd` and `xlwt` are fairly mature but don't work on the Excel 2007 format. – Li-aung Yip Apr 19 '12 at 11:46
  • Ah ok, we use CSV files to introduce first year UG's into python file I/O, so I've only ever done simplistic string parsing with them. That's good to know though! – n00dle Apr 19 '12 at 11:47
  • 1
    The built-in `csv` module handles a lot of the edge cases that a naive parser doesn't. Commas inside quoted strings, oddities of the CSV format written by Excel, that kind of thing. It's OK to write your own CSV parser as a learning exercise, but production code should always use the `csv` module. – Li-aung Yip Apr 19 '12 at 11:49
  • Why the historical downvote, 4 years after I made this post?! – n00dle May 24 '16 at 15:22