我正在尝试 Openpyxl,并编写了以下代码:
from openpyxl import load_workbook, __version__
workbook = load_workbook(filename="Contacts.xlsx")
sheet = workbook.active
就是这样,没有其他代码。
运行时出现以下错误: (TL;DR,由于第 2 行,我得到 “类型错误:__init__() 获得意外的关键字参数 'extLst'”)
"C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\Scripts\python.exe" "C:/Users/taijee/Documents/PythonAll/Python Scripts/newfile.py"
Traceback (most recent call last):
File "C:/Users/taijee/Documents/PythonAll/Python Scripts/newfile.py", line 2, in <module>
workbook = load_workbook(filename="Contacts.xlsx")
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\reader\excel.py", line 315, in load_workbook
reader.read()
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\reader\excel.py", line 279, in read
apply_stylesheet(self.archive, self.wb)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\stylesheet.py", line 192, in apply_stylesheet
stylesheet = Stylesheet.from_tree(node)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\stylesheet.py", line 102, in from_tree
return super(Stylesheet, cls).from_tree(node)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree
obj = desc.from_tree(el)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in from_tree
return [self.expected_type.from_tree(el) for el in node]
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\descriptors\sequence.py", line 85, in <listcomp>
return [self.expected_type.from_tree(el) for el in node]
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\fills.py", line 64, in from_tree
return PatternFill._from_tree(child)
File "C:\Users\taijee\Documents\PythonAll\Python Scripts\venv\lib\site-packages\openpyxl\styles\fills.py", line 102, in _from_tree
return cls(**attrib)
TypeError: __init__() got an unexpected keyword argument 'extLst'
Process finished with exit code 1
我搜索了类似的问题并找到了一个,但它是从 2014 年开始的,所以给出的解决方案是下载 Openpyxl 2.0 版本而不是 2.2。 openpyxl版本是3.0.4,python版本是3.8.3,Contacts.xlsx肯定存在于同一个文件夹中。
编辑:
制作一个新文件已经成功,但我最近使用 openpyxl 制作了另一个程序。它工作正常,但突然崩溃了,给出了与上面的错误非常相似的错误(当时我没有记录它,但最后一行 TypeError: __init__() gets an Unexpected keywords argument 'extLst'
绝对匹配)。
在成功运行和不成功运行之间,我没有对程序进行任何更改,因此我断定该文件有问题。我曾经打开并关闭过该文件一次,但我不记得下一次运行是否是文件损坏时的那次。错误出现后,当我打开文件时,我无法保存任何更改 - 它给出了某种错误。
注意:我没有 Office,因此我在需要时使用 SoftMaker Office Suite 的 Planmaker 打开 .xlsx 文件。
我删除了该文件并创建了一个同名的新文件,以便不更改代码,但新文件的问题仍然存在,包括无法保存更改。
只有当我创建一个具有不同名称的新文件时,代码才起作用。
这甚至可能不是一个 openpyxl 问题,而是一个 Planmaker 问题,尽管我不明白为什么这个错误会一直出现。
尽管如此,如果有人能解释为什么会发生这种情况或如何解决它,我将非常感激。 同时,我会看看我从未使用过 openpyxl 的文件是否仍然会给我带来同样的问题,如果确实如此,在程序中使用 openpyxl 时是否会给出与该文件相同的错误。
最佳答案
我也在使用 SoftMaker Office Suite 的 Planmaker,当我尝试使用 openpyxl 编辑我的 .xlsx 之一时遇到了同样的错误。我发现的唯一解决方案是不使用 Planmaker 并返回 MS Excel。
尽管我不像其他人那样喜欢 MS Excel,但它与 openpyxl 配合得很好。
可能不是您寻求的解决方案,但至少对我来说是一个解决方案。
关于python - 使用 Openpyxl 读取文件时出现类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62760900/