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

时间:2017-09-11 08:05:04

标签: python excel windows openpyxl xlutils

我尝试使用模块openpyxl中的load_workbook打开xlsx文件。我的代码是:

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.

我收到的完整错误消息是:

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'>

任何人都知道这是什么?

1 个答案:

答案 0 :(得分:0)

我收到了同样的错误。就我而言,Excel工作簿没有图表,没有过滤器,没有公式,没有VBA。仅数据。该工作簿是由某些第三方软件生成的。

原来该工作簿已损坏。在进行一些非常小的更改后,我试图保存它时发现了这一点。解决问题后(通过让Excel按照要求将其保存为其他名称),openpyxl错误消失了。