How to get the inputs from excel and use those inputs in python.
-
2Duplicate: http://stackoverflow.com/questions/1108428/how-do-i-read-a-date-in-excel-format-in-python – S.Lott Sep 22 '09 at 12:27
4 Answers
Take a look at xlrd
This is the best reference I found for learning how to use it: http://www.dev-explorer.com/articles/excel-spreadsheets-and-python

- 81,303
- 11
- 141
- 189

- 4,428
- 4
- 24
- 28
-
2The link you gave is to xlrd version 0.5.2 which is seriously antique! Fortunately PyPI now tells humans that a later version is available, but I'm not sure what happens with go-fetch gadgets like setuptools. Please edit the link to remove the `/0.5.2` from the end. – John Machin Sep 22 '09 at 22:32
Not sure if this is exactly what you're talking about, but:
If you have a very simple excel file (i.e. basically just one table filled with string-values, nothing fancy), and all you want to do is basic processing, then I'd suggest just converting it to a csv (comma-seperated value file). This can be done by "saving as..." in excel and selecting csv.
This is just a file with the same data as the excel, except represented by lines seperated with commas: cell A:1, cell A:2, cell A:3 cell B:1, cell B:2, cell b:3
This is then very easy to parse using standard python functions (i.e., readlines to get each line of the file, then it's just a list that you can split on ",").
This if of course only helpful in some situations, like when you get a log from a program and want to quickly run a python script which handles it.
Note: As was pointed out in the comments, splitting the string on "," is actually not very good, since you run into all sorts of problems. Better to use the csv module (which another answer here teaches how to use).

- 9,772
- 17
- 62
- 92
-
I agree it's sometimes better to convert excel file into csv (google for xls2csv), but why split the results by ","? it's a lot better to use built-in csv module. what'd you do if csv contains such line: 1, 2, "abc", 3, "string, with comma", 5? it'll be splitted into 7 parts instead of 6! – uolot Sep 22 '09 at 13:31
-
Honestly, for two reasons: to keep things simple, and because I never learned how to use the csv module (the simple solution always worked for me). You're probably right that it's worth learning, so I'll edit the answer. – Edan Maor Sep 22 '09 at 15:29
-
Hello hello ... save as csv is fine if you have (as you say) only one table and only string values -- in which case why would you be using Excel anyway?? Anything more complicated and you get all sorts of problems -- this was one of my primary motivations in writing xlrd ;-) – John Machin Sep 22 '09 at 22:43
-
Actually I never heard about xlrd 'til this question, kinda wish I'd heard about it earlier :) But here is an example of where I used the above method: I had a program (a network analyzer) which gave out log files as excel files. I then wanted to parse these logs and use the data to compute interesting things. The easiest way for me for this quick n' dirty job, was to perform the above. Like I said, this is obviously not a good long-term solution, just a dirty trick to get something done quickly. I'll be sure to check out xlrd next time I need to parse excels, though :) – Edan Maor Sep 23 '09 at 01:22
import win32com
Excel=win32com.client.Dispatch("Excel.Application")
Excel.Workbooks.Open(file path)
Cells=Excel.ActiveWorkBook.ActiveSheet.Cells
Cells(row,column).Value=Input
Output=Cells(row,column).Value

- 129
- 1
- 3
- 7
If you can save as a csv file with headers:
Attrib1, Attrib2, Attrib3
value1.1, value1.2, value1.3
value2,1,...
Then I would highly recommend looking at built-in the csv module
With that you can do things like:
csvFile = csv.DictReader(open("csvFile.csv", "r"))
for row in csvFile:
print row['Attrib1'], row['Attrib2']

- 2,557
- 5
- 26
- 32