【问题标题】:TypeError while using Openpyxl to read file使用 Openpyxl 读取文件时出现 TypeError
【发布时间】:2020-10-26 20:43:18
【问题描述】:

我正在试用 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 是否会出现与该文件相同的错误。

【问题讨论】:

  • 您的文件有问题,因为图案填充没有任何扩展名。
  • 您能告诉我们您的文件所在的目录吗?通常,这些类型的错误来自文件所在位置与您指向的位置之间的不匹配。您可以使用import os print(os.getcwd()) 检查 python 正在查看的目录
  • 好的,@jmb,我会检查一下。
  • @Charlie Clark,我会新建一个文件试试看。

标签: python excel openpyxl


【解决方案1】:

我遇到了同样的错误“ TypeError: init() 得到了一个意外的关键字参数 'extLst' " 因为我使用 Planmaker 软件更改并保存了 xlsx 格式的表格工作簿文件。

之后我用 libreoffice calc 保存了这个文件。然后用 openpyxl 加载这个 xlsx 文件就可以了。

对于某些用户来说,这可能是一种解决方法。

【讨论】:

    【解决方案2】:

    我也在使用 SoftMaker Office Suite 的 Planmaker,当我尝试使用 openpyxl 编辑我的一个 .xlsx 时遇到了同样的错误。我找到的唯一解决方案是不使用 Planmaker 并返回 MS Excel。

    尽管我不喜欢 MS Excel,但它与 openpyxl 完美结合。

    可能不是您寻求的解决方案,但至少对我来说是一个解决方案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 2018-08-23
      • 2018-11-13
      • 1970-01-01
      相关资源
      最近更新 更多