Ryflex Ryflex - 1 year ago 177
Python Question

Opening and reading an excel .xlsx file in python

I'm trying to open an excel .xlsx file with python but am unable to find a way to do it, I've tried using pandas but it's wanting to use a library called NumPy I've tried to install numpy but it still can't find numpy.

I've also tried using the xlrd library but I get the following traceback:

Traceback (most recent call last):
File "C:\test.py", line 3, in <module>
book = open_workbook('test.xlsx')
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 370, in open_workbook
biff_version = bk.getbof(XL_WORKBOOK_GLOBALS)
File "C:\Python27\lib\site-packages\xlrd\__init__.py", line 1323, in getbof
raise XLRDError('Expected BOF record; found 0x%04x' % opcode)
XLRDError: Expected BOF record; found 0x4b50

Which I assume is because XLRD can't read .xlsx files?

Anyone got any ideas?


import csv
with open('test.csv', 'rb') as csvfile:
data = csv.reader(csvfile, delimiter=',')
for row in data:
print "------------------"
print row
print "------------------"
for cell in row:
print cell

Answer Source

Maybe you could export your .xlsx to a .csv file?

Then you could try:

import csv
with open('file.csv','rb') as file:
    contents = csv.reader(file)
    [x for x in contents]

This may be useful: http://docs.python.org/2/library/csv.html#csv.reader

Hope that helps!


If you want to locate a spectific cell, such as F13, you could make a nested list like a matrix and them refer to each element:

import csv
with open('file.csv','rb') as file:
    contents = csv.reader(file)
    matrix = list()
    for row in contents:

And then access F13 with matrix[5][12].

P.S.: I did not test this. If "row" is a list with each cell as an element, you keep appending all lines to the matrix, so the first index is row number and the second is the column number.