简体   繁体   English

尝试从 openpyxl 使用模块 load_workbook 时出错

[英]Error when trying to use module load_workbook from openpyxl

I'm attempting to open an xlsx-file using load_workbook from the module openpyxl .我正在尝试使用模块openpyxl 中的 load_workbook打开一个 xlsx 文件。 The code I have is:我的代码是:

import os
from openpyxl import load_workbook

def edit_workbook():
    path = r'C:\123 ABC\Excel documents'
    filename = 'filename.xlsx'

    os.path.join(path, filename)
    workbook = load_workbook(os.path.join(path, filename))

    ## Error is on the line above.

The complete error message I get is:我得到的完整错误消息是:

Traceback (most recent call last):

  File "<ipython-input-12-22dfdfc4e5e1>", line 1, in <module>
    workbook = load_workbook(os.path.join(path, filename))

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\reader\excel.py", line 214, in load_workbook
    apply_stylesheet(archive, wb) # bind styles to workbook

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\styles\stylesheet.py", line 176, in apply_stylesheet
    stylesheet = Stylesheet.from_tree(node)

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\styles\stylesheet.py", line 99, in from_tree
    return super(Stylesheet, cls).from_tree(node)

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
    obj = desc.expected_type.from_tree(el)

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
    obj = desc.expected_type.from_tree(el)

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 79, in from_tree
    obj = desc.expected_type.from_tree(el)

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\serialisable.py", line 92, in from_tree
    return cls(**attrib)

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\styles\table.py", line 37, in __init__
    self.dxfId = dxfId

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\base.py", line 69, in __set__
    value = _convert(self.expected_type, value)

  File "C:\Users\Stewie\Anaconda3\lib\site-packages\openpyxl\descriptors\base.py", line 59, in _convert
    raise TypeError('expected ' + str(expected_type))

TypeError: expected <class 'int'>

Anyone know what this can be?有谁知道这可能是什么?

I received the same error.我收到了同样的错误。 In my case, the Excel workbook has no charts, no filters, no formulas, no VBA.就我而言,Excel 工作簿没有图表、没有过滤器、没有公式、没有 VBA。 Only data.只有数据。 This workbook was generated by some third party software.此工作簿由某些第三方软件生成。

Turns out that the workbook was corrupted.原来工作簿已损坏。 I found that out when trying to save it after some very minor change.我在一些非常小的更改后尝试保存它时发现了这一点。 Once I resolved the corruption (by letting Excel save it to a different name as it requested), the openpyxl error disappeared.一旦我解决了损坏问题(通过让 Excel 按照要求将其保存为不同的名称),openpyxl 错误就消失了。

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

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