Cannot read excel files when custom document property has no `name` attribute
Openpyxl version: 3.1.2
I am trying to read an excel using load_workbook and getting the following error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/home/sourabh/test2/.venv/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 346, in load_workbook
reader.read()
File "/home/sourabh/test2/.venv/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 295, in read
self.read_custom()
File "/home/sourabh/test2/.venv/lib/python3.10/site-packages/openpyxl/reader/excel.py", line 182, in read_custom
self.wb.custom_doc_props = CustomPropertyList.from_tree(src)
File "/home/sourabh/test2/.venv/lib/python3.10/site-packages/openpyxl/packaging/custom.py", line 220, in from_tree
new_prop = typ(name=prop.name, value=value)
File "/home/sourabh/test2/.venv/lib/python3.10/site-packages/openpyxl/packaging/custom.py", line 136, in __init__
self.name = name
File "/home/sourabh/test2/.venv/lib/python3.10/site-packages/openpyxl/descriptors/base.py", line 46, in __set__
raise TypeError(msg)
TypeError: <class 'openpyxl.packaging.custom.StringProperty'>.name should be <class 'str'> but value is <class 'NoneType'>
On examining the docProps/custom.xml
file, I saw a property without the name attribute and only a value. I am able to open this file in MS-Excel and Libreoffice without any issues. Is there any possibility that a fix can be added to skip custom document properties without the name attribute?
# openpyxl/packaging/custom.py:CustomPropertyList
@classmethod
def from_tree(cls, tree):
"""
Create list from OOXML element
"""
prop_list = _CustomDocumentPropertyList.from_tree(tree)
new_props = cls()
for prop in prop_list.property:
attr = prop.type
typ = XML_MAPPING.get(attr, None)
if not typ:
warn(f"Unknown type for {prop.name}")
continue
value = getattr(prop, attr)
link = prop.linkTarget
if link is not None:
typ = LinkProperty
value = prop.linkTarget
if prop.name is None: # patch for skipping invalid property
warn(f"Skipping unnamed custom property with value {value}")
continue
new_prop = typ(name=prop.name, value=value)
new_props.append(new_prop)
return new_props