Opening and reading excel.xlsx file in python

I am trying to open excel.xlsx file with python, but cannot find a way to do this, I tried using pandas, but he wanted to use a library called NumPy. I tried to install numpy, but it still cannot find numpy.

I also tried using the xlrd library, but I get the following trace:

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 

What do I assume because XLRD cannot read .xlsx files?

Does anyone have 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 
+6
source share
3 answers

Perhaps you can export the .xlsx to a CSV file?

Then you can try:

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

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

Hope this helps!

EDIT:

If you want to find a specialized cell, such as F13, you can make a nested list as a matrix, and they apply 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 enter F13 with matrix[5][12] .

PS: I have not tested this. If the "row" is a list with each cell as an element, you add all the rows to the matrix, so the first index is the row number and the second is the column number.

+11
source

You seem to be on Linux Distro. I had the same problem and this does not happen with the "xlwt" library, but only with "xlrd". what I did is not the right way to solve this problem, but it makes me work all the time to hope that the answer to this question comes up soon, I installed "xlrd" on Windows and took the folder and inserted it on Linux in where is my python code and it worked.

+2
source

Since I know that other people will read this too -

You can install the following module (it does not exist automatically) https://pypi.python.org/pypi/openpyxl

You can read the following to get a good analysis of how to use it.

https://automatetheboringstuff.com/chapter12/

+2
source

Source: https://habr.com/ru/post/952829/


All Articles