简体   繁体   English

Openpyxl 无法读取 xlsx 文件,但如果我保存该文件,它会打开

[英]Openpyxl can't read xlsx file, but if i save the file, it opens

So, I tried to open an excel file with openpyxl with this line所以,我试图用这一行用 openpyxl 打开一个 excel 文件

wb_bs = openpyxl.load_workbook(filename=filepath)

And got this error:并得到这个错误:


C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\styles\stylesheet.py:214: UserWarning: Workbook contains no default style, apply openpyxl's default
  warn("Workbook contains no default style, apply openpyxl's default")
Traceback (most recent call last):
    wb_bs = openpyxl.load_workbook(filename=url_nova, data_only=True)
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
    reader.read()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\reader\excel.py", line 280, in read
    self.read_worksheets()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\reader\excel.py", line 228, in read_worksheets
    ws_parser.bind_all()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\worksheet\_reader.py", line 434, in bind_all
    self.bind_cells()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\worksheet\_reader.py", line 337, in bind_cells
    for idx, row in self.parser.parse():
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\worksheet\_reader.py", line 149, in parse
    obj = prop[1].from_tree(element)
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 87, in from_tree
    obj = desc.expected_type.from_tree(el)
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree
    return cls(**attrib)
TypeError: __init__() got an unexpected keyword argument 'address'
PS C:\Users\T-Gamer\Python programs\cmtrat\Cmtrat Helper> & C:/Users/T-Gamer/AppData/Local/Programs/Python/Python38-32/python.exe "c:/Users/T-Gamer/Python programs/cmtrat/Cmtrat Helper/excel_scripts/ostest.py"
C:\Users\T-Gamer\Python programs\cmtrat\Cmtrat Helper\excel_scripts\copias\diario_padrao.xlsx
C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\styles\stylesheet.py:214: UserWarning: Workbook contains no default style, apply openpyxl's default
  warn("Workbook contains no default style, apply openpyxl's default")
Traceback (most recent call last):
    wb_bs = openpyxl.load_workbook(filename=url_nova, data_only=True)
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
    reader.read()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\reader\excel.py", line 280, in read
    self.read_worksheets()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\reader\excel.py", line 228, in read_worksheets
    ws_parser.bind_all()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\worksheet\_reader.py", line 434, in bind_all
    self.bind_cells()
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\worksheet\_reader.py", line 337, in bind_cells
    for idx, row in self.parser.parse():
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\worksheet\_reader.py", line 149, in parse
    obj = prop[1].from_tree(element)
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 87, in from_tree
    obj = desc.expected_type.from_tree(el)
  File "C:\Users\T-Gamer\AppData\Local\Programs\Python\Python38-32\lib\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree
    return cls(**attrib)
TypeError: __init__() got an unexpected keyword argument 'address'

The thing is:事情是:

  • If I create the .xlsx file, it opens如果我创建 .xlsx 文件,它会打开
  • If I download the file from this specific source(the one I need) and try to open it straight away, it generates the error.如果我从该特定来源(我需要的来源)下载文件并尝试立即打开它,则会生成错误。
  • If I run the code after I open and save the .xlsx file(no changes), it works.如果我在打开并保存 .xlsx 文件(无更改)后运行代码,它会起作用。

I suppose it has something to do with excel version conflict, but I've tried everything and nothing seems to work.我想这与 excel 版本冲突有关,但我已经尝试了所有方法,但似乎没有任何效果。

openpyxl==3.0.5 python==3.8.5 openpyxl==3.0.5 python==3.8.5

This seems to be an Openpyxl bug regarding some Excel files, reported here: https://foss.heptapod.net/openpyxl/openpyxl/-/issues/1071这似乎是关于某些 Excel 文件的 Openpyxl 错误,在此处报告: https ://foss.heptapod.net/openpyxl/openpyxl/-/issues/1071

Unfortunately it looks like there's no fix, just the workaround you've found:不幸的是,看起来没有修复,只是您找到的解决方法:

In the just released version of Excel (Version 1803 (Build 9126.2259 Click-to-Run)), Microsoft has modified the way hyperlinks are stored in Excel files.在刚刚发布的 Excel 版本(版本 1803(内部版本 9126.2259 即点即用))中,Microsoft 修改了超链接在 Excel 文件中的存储方式。 As a workaround, you could try opening and re-saving the file in Google Sheets or LibreOffice.作为解决方法,您可以尝试在 Google 表格或 LibreOffice 中打开并重新保存文件。 I am experiencing similar issues with data validation.我在数据验证方面遇到了类似的问题。
Source 来源

The reason may be the security prevention of MS-Windows: Whenever you download an MS-Office file from an outer source (internet), MS-Windows inserts a flag in that file which marks the file to be opened in protected view only.原因可能是 MS-Windows 的安全防护:每当您从外部源(互联网)下载 MS-Office 文件时,MS-Windows 都会在该文件中插入一个标志,该标志将文件标记为仅在受保护的视图中打开。 That protection stays still until you enable editing and save the file with the security flag set off.在您启用编辑并在设置安全标志的情况下保存文件之前,该保护将保持不变。

The warning text that appears when you open a newly downloaded MS-Office file:打开新下载的 MS-Office 文件时出现的警告文本:

PROTECTED VIEW
Be careful - files from the Internet can contain viruses.
Unless you need to edit, it's safer to stay in Protected View.

I was facing the same problem.我面临着同样的问题。 I meant to do something with pandas.read_excel() , and it return Empty Dataframe if I don't open and save the xlsx manually.我打算用pandas.read_excel()做一些事情,如果我不手动打开和保存 xlsx,它会返回Empty Dataframe So here's my code, at least it works now…所以这是我的代码,至少它现在可以工作......

import openpyxl
import pandas as pd

file_path = r’t.xlsx’
def foo():
    return pd.read_excel(file_path)

data = foo()
if data.emty:
    writer = openpyxl.load_workbook(file_path)
    writer.save(file_path) # simply re-save it
    data = foo()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM