首页 > 解决方案 > openpyxl:ValueError:值必须是 {'selection'、'data'、'field'} 之一

问题描述

有一个 xlsm 文件,我需要根据工作表中启用的宏打开、编辑和提取数据。

但我无法打开文件本身。我试过了:

wb = openpyxl.load_workbook("workbook.xlsm",read_only=False,keep_vba=True)

发生错误:

Traceback (most recent call last):
  File "C:/Users/Downloads/Projects/Project1/Trial4.py", line 7, in <module>
    wb = openpyxl.load_workbook("workbook.xlsm",read_only=False,keep_vba=True)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\reader\excel.py", line 317, in load_workbook
    reader.read()
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\reader\excel.py", line 282, in read
    self.read_worksheets()
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\reader\excel.py", line 268, in read_worksheets
    pivot = TableDefinition.from_tree(tree)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\descriptors\serialisable.py", line 83, in from_tree
    obj = desc.from_tree(el)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\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\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\descriptors\sequence.py", line 85, in <listcomp>
    return [self.expected_type.from_tree(el) for el in node]
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\descriptors\serialisable.py", line 103, in from_tree
    return cls(**attrib)
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\pivot\table.py", line 481, in __init__
    self.scope = scope
  File "C:\Users\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.7_qbz5n2kfra8p0\LocalCache\local-packages\Python37\site-packages\openpyxl\descriptors\base.py", line 128, in __set__
    raise ValueError(self.__doc__)
ValueError: Value must be one of {'selection', 'data', 'field'}

有人可以为这个问题提供解决方案。TIA

标签: pythonpython-3.xexcelvbaopenpyxl

解决方案


我有同样的问题没有在互联网上找到解决方案,所以我解构了 openpyxl,我终于找到了。

似乎缺少一段代码来管理“ConditionalFormat”,因此我将其全部从源 excel 中删除。

这似乎解决了这个问题。


推荐阅读