Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.0.1 - Fix namespace issues with Custom Properties #26

Merged
merged 11 commits into from
Oct 11, 2024
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
Release History
---------------

1.0.1 (2024-10-11)
++++++++++++++++++

- DEV-4268: Fix namespace issues with Custom Properties raising Unreadable Content error for MS Word users


1.0.0 (2024-05-23)
++++++++++++++++++

Expand Down
Binary file added dist/python_docx_bb-1.0.1-py3-none-any.whl
Binary file not shown.
2 changes: 1 addition & 1 deletion src/docx/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
if TYPE_CHECKING:
from docx.opc.part import Part

__version__ = "1.0.0"
__version__ = "1.0.1"


__all__ = ["Document"]
Expand Down
15 changes: 7 additions & 8 deletions src/docx/opc/customprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@

import numbers
from lxml import etree

NS_VT = "http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"
from docx.oxml.ns import nspfxmap, qn


class CustomProperties(object):
Expand All @@ -26,12 +25,12 @@ def __getitem__(self, item):
prop = self.lookup(item)
if prop is not None:
elm = prop[0]
if elm.tag == f"{{{NS_VT}}}i4":
if elm.tag == qn("vt:i4"):
try:
return int(elm.text)
except ValueError:
return elm.text
elif elm.tag == f"{{{NS_VT}}}bool":
elif elm.tag == qn("vt:bool"):
return True if elm.text == '1' else False
return elm.text

Expand All @@ -45,8 +44,8 @@ def __setitem__(self, key, value):
elif isinstance(value, numbers.Number):
elm_type = 'i4'
value = str(int(value))
prop = etree.SubElement(self._element, "property")
elm = etree.SubElement(prop, f"{{{NS_VT}}}{elm_type}", nsmap={'vt':NS_VT})
prop = etree.SubElement(self._element, qn("op:property"), nsmap=nspfxmap("op"))
elm = etree.SubElement(prop, qn(f"vt:{elm_type}"), nsmap=nspfxmap("vt"))
elm.text = value
prop.set("name", key)
# magic number "FMTID_UserDefinedProperties"
Expand All @@ -55,9 +54,9 @@ def __setitem__(self, key, value):
prop.set("pid", str(len(self._element) + 1))
else:
elm = prop[0]
if elm.tag == f"{{{NS_VT}}}i4":
if elm.tag == qn("vt:i4"):
elm.text = str(int(value))
elif elm.tag == f"{{{NS_VT}}}bool":
elif elm.tag == qn("vt:bool"):
elm.text = str(1 if value else 0)
else:
elm.text = str(value)
Expand Down
2 changes: 1 addition & 1 deletion src/docx/oxml/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@

from .customprops import CT_CustomProperties # noqa

register_element_cls('cup:Properties', CT_CustomProperties)
register_element_cls('op:Properties', CT_CustomProperties)

from .document import CT_Body, CT_Document # noqa

Expand Down
6 changes: 2 additions & 4 deletions src/docx/oxml/customprops.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,19 @@
from docx.oxml import parse_xml



class CT_CustomProperties(BaseOxmlElement):
"""
``<Properties>`` element, the root element of the Custom Properties
part stored as ``/docProps/custom.xml``. String elements are
limited in length to 255 unicode characters.
"""

_customProperties_tmpl = "<cup:Properties %s/>\n" % nsdecls("cup", "vt")
_customProperties_tmpl = "<op:Properties %s/>\n" % nsdecls("op", "vt")
_offset_pattern = re.compile("([+-])(\\d\\d):(\\d\\d)")

@classmethod
def new(cls):
"""
Return a new ``<property>`` element
Return a new ``<op:Properties>`` element
"""
xml = cls._customProperties_tmpl
custom_properties = parse_xml(xml)
Expand Down
2 changes: 1 addition & 1 deletion src/docx/oxml/ns.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"a": "http://schemas.openxmlformats.org/drawingml/2006/main",
"c": "http://schemas.openxmlformats.org/drawingml/2006/chart",
"cp": "http://schemas.openxmlformats.org/package/2006/metadata/core-properties",
"cup": "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
"op": "http://schemas.openxmlformats.org/officeDocument/2006/custom-properties",
"dc": "http://purl.org/dc/elements/1.1/",
"dcmitype": "http://purl.org/dc/dcmitype/",
"dcterms": "http://purl.org/dc/terms/",
Expand Down