尝试从 openpyxl 使用模块 load_workbook 时出错 [英] Error when trying to use module load_workbook from openpyxl

查看:389
本文介绍了尝试从 openpyxl 使用模块 load_workbook 时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用模块中的 load_workbook 打开一个 xlsx 文件 openpyxl.我的代码是:

I'm attempting to open an xlsx-file using load_workbook from the module openpyxl. 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'>

有人知道这是什么吗?

推荐答案

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

I received the same error. In my case, the Excel workbook has no charts, no filters, no formulas, no VBA. Only data. This workbook was generated by some third party software.

结果是工作簿已损坏.我在一些非常小的更改后尝试保存它时发现了这一点.一旦我解决了损坏问题(通过让 Excel 按照要求将其保存为不同的名称),openpyxl 错误就消失了.

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.

这篇关于尝试从 openpyxl 使用模块 load_workbook 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆