From 0eccb7a40f20a82694a4afe67da97aefee3cceb7 Mon Sep 17 00:00:00 2001 From: Svapnesh Patel Date: Tue, 16 Jan 2024 15:42:33 +0530 Subject: [PATCH] Support for endnotes --- docxcompose/composer.py | 46 ++++++++++++++++++++++++++++++ docxcompose/templates/endnotes.xml | 2 ++ 2 files changed, 48 insertions(+) create mode 100644 docxcompose/templates/endnotes.xml diff --git a/docxcompose/composer.py b/docxcompose/composer.py index ae530a5..52e5cfa 100644 --- a/docxcompose/composer.py +++ b/docxcompose/composer.py @@ -27,6 +27,7 @@ PART_RELTYPES_WITH_STYLES = [ RT.FOOTNOTES, + RT.ENDNOTES ] @@ -84,6 +85,7 @@ def insert(self, index, doc, remove_property_fields=True): self.add_diagrams(doc, element) self.add_shapes(doc, element) self.add_footnotes(doc, element) + self.add_endnotes(doc, element) self.remove_header_and_footer_references(doc, element) index += 1 @@ -701,3 +703,47 @@ def fix_header_and_footers(self, doc): first_sect_pr.append(pg_num_type) self.first_section_properties_added = True + + def add_endnotes(self, doc, element): + """Add endnotes from the given document used in the given element.""" + endnotes_refs = element.findall('.//w:endnoteReference', NS) + + if not endnotes_refs: + return + + endnote_part = doc.part.rels.part_with_reltype(RT.ENDNOTES) + + my_endnote_part = self.endnote_part() + + endnotes = parse_xml(my_endnote_part.blob) + next_id = len(endnotes) + 1 + + for ref in endnotes_refs: + id_ = ref.get('{%s}id' % NS['w']) + element = parse_xml(endnote_part.blob) + endnote = deepcopy(element.find('.//w:endnote[@w:id="%s"]' % id_, NS)) + endnotes.append(endnote) + endnote.set('{%s}id' % NS['w'], str(next_id)) + ref.set('{%s}id' % NS['w'], str(next_id)) + next_id += 1 + + self.add_referenced_parts(endnote_part, my_endnote_part, element) + + my_endnote_part._blob = serialize_part_xml(endnotes) + + def endnote_part(self): + """The endnote part of the document.""" + try: + endnote_part = self.doc.part.rels.part_with_reltype(RT.ENDNOTES) + except KeyError: + # Create a new empty footnotes part + partname = PackURI('/word/endnotes.xml') + content_type = CT.WML_ENDNOTES + xml_path = os.path.join( + os.path.dirname(__file__), 'templates', 'endnotes.xml') + with open(xml_path, 'rb') as f: + xml_bytes = f.read() + endnote_part = Part( + partname, content_type, xml_bytes, self.doc.part.package) + self.doc.part.relate_to(endnote_part, RT.ENDNOTES) + return endnote_part diff --git a/docxcompose/templates/endnotes.xml b/docxcompose/templates/endnotes.xml new file mode 100644 index 0000000..451e855 --- /dev/null +++ b/docxcompose/templates/endnotes.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file