From 049572ea09094c071b91d856d6af68403b48fd4f Mon Sep 17 00:00:00 2001
From: Stefano Cipriani <stefano.cipriani@ag2.it>
Date: Tue, 1 Oct 2024 10:16:25 +0200
Subject: [PATCH 1/2] fix zipfile.py 'UserWarning: Duplicate name:
 docProps/core.xml'

---
 src/docx/opc/constants.py | 4 ++++
 src/docx/opc/package.py   | 9 ++++++---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/src/docx/opc/constants.py b/src/docx/opc/constants.py
index 89d3c16cc..c304cf3d7 100644
--- a/src/docx/opc/constants.py
+++ b/src/docx/opc/constants.py
@@ -313,6 +313,10 @@ class RELATIONSHIP_TYPE:
         "http://schemas.openxmlformats.org/package/2006/relationships/metada"
         "ta/core-properties"
     )
+    CORE_PROPERTIES_OFFICEDOCUMENT = (
+        "http://schemas.openxmlformats.org/officedocument/2006/relationships"
+        "/metadata/core-properties"
+    )
     CUSTOM_PROPERTIES = (
         "http://schemas.openxmlformats.org/officeDocument/2006/relationships"
         "/custom-properties"
diff --git a/src/docx/opc/package.py b/src/docx/opc/package.py
index 3b1eef256..e8a10bac5 100644
--- a/src/docx/opc/package.py
+++ b/src/docx/opc/package.py
@@ -175,9 +175,12 @@ def _core_properties_part(self) -> CorePropertiesPart:
         try:
             return cast(CorePropertiesPart, self.part_related_by(RT.CORE_PROPERTIES))
         except KeyError:
-            core_properties_part = CorePropertiesPart.default(self)
-            self.relate_to(core_properties_part, RT.CORE_PROPERTIES)
-            return core_properties_part
+            try:
+                return cast(CorePropertiesPart, self.part_related_by(RT.CORE_PROPERTIES_OFFICEDOCUMENT))
+            except KeyError:
+                core_properties_part = CorePropertiesPart.default(self)
+                self.relate_to(core_properties_part, RT.CORE_PROPERTIES)
+                return core_properties_part
 
 
 class Unmarshaller:

From bbe7d62bd6792461ba33741666fa9eb66c7f9981 Mon Sep 17 00:00:00 2001
From: Stefano Cipriani <stefano.cipriani@ag2.it>
Date: Mon, 7 Oct 2024 18:02:33 +0200
Subject: [PATCH 2/2] remap core-properties namespace from ../officedocument/..
 to ../package/..

---
 src/docx/opc/package.py | 5 ++++-
 src/docx/opc/rel.py     | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/src/docx/opc/package.py b/src/docx/opc/package.py
index e8a10bac5..29c8eb05f 100644
--- a/src/docx/opc/package.py
+++ b/src/docx/opc/package.py
@@ -176,7 +176,10 @@ def _core_properties_part(self) -> CorePropertiesPart:
             return cast(CorePropertiesPart, self.part_related_by(RT.CORE_PROPERTIES))
         except KeyError:
             try:
-                return cast(CorePropertiesPart, self.part_related_by(RT.CORE_PROPERTIES_OFFICEDOCUMENT))
+                office_document_part = self.part_related_by(RT.CORE_PROPERTIES_OFFICEDOCUMENT)
+                rel = self.relate_to(office_document_part, RT.CORE_PROPERTIES_OFFICEDOCUMENT)
+                self.rels[rel].set_reltype(RT.CORE_PROPERTIES)
+                return cast(CorePropertiesPart, office_document_part)
             except KeyError:
                 core_properties_part = CorePropertiesPart.default(self)
                 self.relate_to(core_properties_part, RT.CORE_PROPERTIES)
diff --git a/src/docx/opc/rel.py b/src/docx/opc/rel.py
index 47e8860d8..cd5176d68 100644
--- a/src/docx/opc/rel.py
+++ b/src/docx/opc/rel.py
@@ -134,6 +134,9 @@ def is_external(self) -> bool:
     def reltype(self) -> str:
         return self._reltype
 
+    def set_reltype(self, reltype:str) -> None:
+        self._reltype = reltype
+
     @property
     def rId(self) -> str:
         return self._rId