Can't read excel files, using openpyxl

你离开我真会死。 提交于 2019-12-07 04:09:26

Depending on which version you are using, this could be a bug in openpyxl. For example, in 1.6.1 a bug was introduced exhibiting this behavior. Reverting to 1.5.8 fixed it. There was a fix according to this openpyxl ticket; though the ticket doesn't say when the fix was delivered, it was committed in early 2013. I upgraded to 1.6.2 and the error went away.

You can use xlrd biblioteque

This script allow you to transform a excel data to list of dictionnaries

import xlrd

workbook = xlrd.open_workbook('your_file.xlsx')
workbook = xlrd.open_workbook('your_file.xlsx', on_demand = True)
worksheet = workbook.sheet_by_index(0)
first_row = [] # The row where we stock the name of the column
for col in range(worksheet.ncols):
    first_row.append( worksheet.cell_value(0,col) )
# tronsform the workbook to a list of dictionnary
data =[]
for row in range(1, worksheet.nrows):
    elm = {}
    for col in range(worksheet.ncols):
        elm[first_row[col]]=worksheet.cell_value(row,col)
    data.append(elm)
print data

I guess your file is .xls format before, you can use

try:
    f1 = load_workbook(filename=f)
except:
    print f

to find which file cause this error and reopen it in Excel, then save as .xlsx.

I found this post searching for a solution to a similar issue, ("There is no item named '[Content_Types].xml' in the archive")

None of this error message makes any sense in terms of my script or the file. My script adds 1 sheet and updates five more in an existing Excel document. While my script was running, I realized I had an error in my code. I canceled my script mid-running.

After canceling, the existing Excel file exhibited this error. Working out bugs with the script, maybe you corrupted your Excel file??

To address this, I'm thinking of creating a temporary restore file in the event of an error using OpenPyXl.

I has the same issue, make sure the file you're trying to read isn't open in Excel already

If openpyxl still doesn't work, using pandas works.

$ pip install pandas xlrd

And this code works:

import pandas as pd

df = pd.read_excel(file_path)

Option 1: I have overcome this issue by adding read_only=True: Specifically, replace

f1 = load_workbook(filename=f) with

f1 = load_workbook(filename=f, read_only=True)

Note: Depending on your code,read_only=True can make your code very slow. If this is the case for you, you may want to try option 2.

Option 2: Open your problematic workbook in excel, and then re-save it as a Strict Open XML Spreadsheet (*.xlsx)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!