Domanda

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?

EDIT:

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
È stato utile?

Soluzione

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!

EDIT:

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:
        matrix.append(row)

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.

Altri suggerimenti

it seems that you are on a Linux Distro. I had the same problem too and this does not happen with "xlwt" library but only with "xlrd". what I did is not the right way to solve this problem but it makes things work for the time being to hopefully have an answer to that question soon ;I have installed "xlrd" on Windows and took the folder and pasted it on Linux in the directory where my python code is and it worked.

Since I know other people will also be reading this -

You can install the following module (it's not there automatically) https://pypi.python.org/pypi/openpyxl

You can read the following to get a nice breakdown on how to use it

https://automatetheboringstuff.com/chapter12/

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top