From c4616e0b42be146586caf98bb9209851e1989e57 Mon Sep 17 00:00:00 2001 From: ryanamannion Date: Mon, 7 Oct 2024 16:03:22 -0400 Subject: [PATCH 1/6] DEV-4268: fix custom-properties creation --- src/docx/oxml/__init__.py | 2 +- src/docx/oxml/customprops.py | 4 ++-- src/docx/oxml/ns.py | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/docx/oxml/__init__.py b/src/docx/oxml/__init__.py index e385badd8..c1b3146e0 100644 --- a/src/docx/oxml/__init__.py +++ b/src/docx/oxml/__init__.py @@ -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 diff --git a/src/docx/oxml/customprops.py b/src/docx/oxml/customprops.py index 506645289..03aa6fcf3 100644 --- a/src/docx/oxml/customprops.py +++ b/src/docx/oxml/customprops.py @@ -23,8 +23,8 @@ class CT_CustomProperties(BaseOxmlElement): part stored as ``/docProps/custom.xml``. String elements are limited in length to 255 unicode characters. """ - - _customProperties_tmpl = "\n" % nsdecls("cup", "vt") + xmlns = 'xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"' + _customProperties_tmpl = "\n" % (xmlns + " " + nsdecls("vt")) _offset_pattern = re.compile("([+-])(\\d\\d):(\\d\\d)") @classmethod diff --git a/src/docx/oxml/ns.py b/src/docx/oxml/ns.py index f179f61fc..89afe8a37 100644 --- a/src/docx/oxml/ns.py +++ b/src/docx/oxml/ns.py @@ -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/", From 988388e6113c3a103c95b4b3828572599699d87c Mon Sep 17 00:00:00 2001 From: ryanamannion Date: Mon, 7 Oct 2024 16:04:04 -0400 Subject: [PATCH 2/6] DEV-4268: commit for 4268-rc0 --- src/docx/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/docx/__init__.py b/src/docx/__init__.py index bbd7267c4..f91f9d225 100644 --- a/src/docx/__init__.py +++ b/src/docx/__init__.py @@ -13,7 +13,8 @@ if TYPE_CHECKING: from docx.opc.part import Part -__version__ = "1.0.0" +# __version__ = "1.0.1-a0" +__version__ = "4268-rc0" __all__ = ["Document"] From c5ebb905c8a6b39ed6895ff226bbffd3b5b897be Mon Sep 17 00:00:00 2001 From: ryanamannion Date: Fri, 11 Oct 2024 13:39:58 -0400 Subject: [PATCH 3/6] change version back --- src/docx/__init__.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/docx/__init__.py b/src/docx/__init__.py index f91f9d225..ba58680cf 100644 --- a/src/docx/__init__.py +++ b/src/docx/__init__.py @@ -13,8 +13,7 @@ if TYPE_CHECKING: from docx.opc.part import Part -# __version__ = "1.0.1-a0" -__version__ = "4268-rc0" +__version__ = "1.0.1-dev" __all__ = ["Document"] From b02df37a3f195b500f177202784e27455aece1d5 Mon Sep 17 00:00:00 2001 From: ryanamannion Date: Fri, 11 Oct 2024 14:15:03 -0400 Subject: [PATCH 4/6] use `op` nsprefix for --- src/docx/oxml/customprops.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/docx/oxml/customprops.py b/src/docx/oxml/customprops.py index 03aa6fcf3..6f1605a59 100644 --- a/src/docx/oxml/customprops.py +++ b/src/docx/oxml/customprops.py @@ -16,21 +16,19 @@ from docx.oxml import parse_xml - class CT_CustomProperties(BaseOxmlElement): """ ```` element, the root element of the Custom Properties part stored as ``/docProps/custom.xml``. String elements are limited in length to 255 unicode characters. """ - xmlns = 'xmlns="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties"' - _customProperties_tmpl = "\n" % (xmlns + " " + nsdecls("vt")) + _customProperties_tmpl = "\n" % nsdecls("op", "vt") _offset_pattern = re.compile("([+-])(\\d\\d):(\\d\\d)") @classmethod def new(cls): """ - Return a new ```` element + Return a new ```` element """ xml = cls._customProperties_tmpl custom_properties = parse_xml(xml) From cc9b387c4bb4d48b7e7402ecdd3a965a2378b7be Mon Sep 17 00:00:00 2001 From: ryanamannion Date: Fri, 11 Oct 2024 14:15:40 -0400 Subject: [PATCH 5/6] add op: nsprefix for also, use qn to get qualified name as opposed to string formatting --- src/docx/opc/customprops.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/docx/opc/customprops.py b/src/docx/opc/customprops.py index 83eea5844..8eaf97168 100644 --- a/src/docx/opc/customprops.py +++ b/src/docx/opc/customprops.py @@ -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): @@ -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:14"): 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 @@ -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" @@ -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) From aea4fbc71bebcc5161c5a741f71c429ff39958a2 Mon Sep 17 00:00:00 2001 From: ryanamannion Date: Fri, 11 Oct 2024 14:25:42 -0400 Subject: [PATCH 6/6] correct typo --- src/docx/opc/customprops.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/docx/opc/customprops.py b/src/docx/opc/customprops.py index 8eaf97168..3b67ed82b 100644 --- a/src/docx/opc/customprops.py +++ b/src/docx/opc/customprops.py @@ -25,7 +25,7 @@ def __getitem__(self, item): prop = self.lookup(item) if prop is not None: elm = prop[0] - if elm.tag == qn("vt:14"): + if elm.tag == qn("vt:i4"): try: return int(elm.text) except ValueError: