Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions docxcompose/composer.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

PART_RELTYPES_WITH_STYLES = [
RT.FOOTNOTES,
RT.ENDNOTES
]


Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
2 changes: 2 additions & 0 deletions docxcompose/templates/endnotes.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:endnotes xmlns:wpc="http://schemas.microsoft.com/office/word/2010/wordprocessingCanvas" xmlns:mo="http://schemas.microsoft.com/office/mac/office/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math" xmlns:v="urn:schemas-microsoft-com:vml" xmlns:wp14="http://schemas.microsoft.com/office/word/2010/wordprocessingDrawing" xmlns:wp="http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing" xmlns:w10="urn:schemas-microsoft-com:office:word" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main" xmlns:w14="http://schemas.microsoft.com/office/word/2010/wordml" xmlns:w15="http://schemas.microsoft.com/office/word/2012/wordml" xmlns:wpg="http://schemas.microsoft.com/office/word/2010/wordprocessingGroup" xmlns:wpi="http://schemas.microsoft.com/office/word/2010/wordprocessingInk" xmlns:wne="http://schemas.microsoft.com/office/word/2006/wordml" xmlns:wps="http://schemas.microsoft.com/office/word/2010/wordprocessingShape" mc:Ignorable="w14 w15 wp14"></w:endnotes>