我正在尝试 Openpyxl,并编写了以下代码:
from openpyxl import load_workbook, __version__
workbook = load_workbook(filename="Contacts.xlsx")
sheet = workbook.active
就是这样,没有其他代码。
运行时出现以下错误: (TL;DR,由于第 2 行,我得到 “TypeError:__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__() got an unexpected keyword argument 'extLst'
肯定匹配)。
在成功运行和不成功运行之间我没有对程序进行任何更改,因此我得出结论是该文件有问题。我曾经打开并关闭过该文件一次,但我不记得下一次运行是否是文件损坏时的那次。错误出现后,当我打开文件时,我无法保存任何更改 - 它给出了某种错误。
注意:我没有 Office,因此我在需要时使用 SoftMaker Office Suite 的 Planmaker 打开 .xlsx 文件。
我删除了该文件并创建了一个同名的新文件,以便不更改代码,但新文件的问题仍然存在,包括无法保存更改。
只有当我创建一个具有不同名称的新文件时,代码才起作用。
它甚至可能不是一个 openpyxl 问题,而是一个 Planmaker 问题,尽管我不明白为什么这个错误会是我不断收到的错误。
尽管如此,如果有人能解释为什么会发生这种情况或如何解决它,我将非常感激。 同时,我会看看我从未使用过 openpyxl 的文件是否仍然会给我带来同样的问题,如果是的话,在程序中使用时 openpyxl 是否会给出与该文件相同的错误。
我也在使用 SoftMaker Office Suite 的 Planmaker,当我尝试使用 openpyxl 编辑我的 .xlsx 之一时遇到了同样的错误。我找到的唯一解决方案是不使用 Planmaker 并返回 MS Excel。
尽管我不像其他人那样喜欢 MS Excel,但它与 openpyxl 完美配合。
可能不是您寻求的解决方案,但至少对我来说是一个解决方案。
我遇到了同样的错误“ 类型错误:init()得到了意外的关键字参数“extLst” “因为我使用 Planmaker 软件更改并保存了 xlsx 格式的表格工作簿文件。
此后我用 libreoffice calc 保存了这个文件。然后用 openpyxl 加载这个 xlsx 文件就可以了。
这可能是某些用户的解决方法。
请注意,以防其他人偶然发现这一点:最新版本(3.0.10)中的 openpyxl 现在似乎会在“意外关键字”上引发错误。在您可以使用警告模块来忽略它之前。不再。 由于我在 Linux 上工作,因此无法选择 MS Excel,因此我重写了最新的 Python 项目以使用 pylightxl 而不是 openpyxl。它比较麻烦,因为它的功能少得多,但它不会因 Softwaker 的扩展而窒息。 – 亨德里克
我在使用 planmaker 时也遇到了这个错误。我在 planmaker 中使用 *.xls 重新保存了该文件,并能够再次在 python 中加载该文件。我知道这不是理想的格式,但在我开始工作之前一直有效。