简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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