Working with Excel files in Python
Here's a simple guide to accessing Excel files with Python. I'm working with Python 2.7 here.
First in Windows command prompt, in the directory containing your Python scripts (see for example, C:\Python27\Scripts), run two modules:
pip install xlrd
pip install xlwt
![](https://static.wixstatic.com/media/af7fa4_baecee23f51b46189f3557e56a17369c~mv2.png/v1/fill/w_829,h_647,al_c,q_90,enc_auto/af7fa4_baecee23f51b46189f3557e56a17369c~mv2.png)
Then in the IDLE (Python GUI) program enter the below script.
1. Import the xlrd module
>>> import xlrd
2. open the workbook
>>> book = xlrd.open_workbook("C:\FooFolder\python\BattingPost.xls")
3. designate the worksheet you want to work with
>>> sheet = book.sheet_by_name("BattingPost")
4. to pull data from a particular cell, assign a name to the cell.
>>> playerid = sheet.cell_value(2,2)
5. the value of the assigned name can be displayed by using the print command.
>>> print playerid
>>> import xlrd >>> book = xlrd.open_workbook("C:\FooFolder\python\BattingPost.xls") >>> sheet = book.sheet_by_name("BattingPost") >>> playerid = sheet.cell_value(2,2) >>> print playerid bradyst01
![](https://static.wixstatic.com/media/af7fa4_86977bfa1e314c6c8727b671b3f41d72~mv2.png/v1/fill/w_980,h_397,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/af7fa4_86977bfa1e314c6c8727b671b3f41d72~mv2.png)
This is a view of the spreadsheet I pulled data from.
![](https://static.wixstatic.com/media/af7fa4_d603a29d086c4041b311bd6a34f22083~mv2.png/v1/fill/w_980,h_402,al_c,q_90,usm_0.66_1.00_0.01,enc_auto/af7fa4_d603a29d086c4041b311bd6a34f22083~mv2.png)