Skip to content

Commit c20b9e2

Browse files
committed
Fixed #26 - Endnotes relationships are not being parsed
1 parent 815acd8 commit c20b9e2

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

ooxml/doc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def _calculate_possible_headers(self):
160160

161161
def reset(self):
162162
self.elements = []
163-
self.relationships = {}
163+
self.relationships = {'document': {}, 'endnotes': {}}
164164
self.footnotes = {}
165165
self.endnotes = {}
166166
self.comments = {}

ooxml/parse.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -471,7 +471,7 @@ def parse_document(xmlcontent):
471471
return document
472472

473473

474-
def parse_relationship(document, xmlcontent):
474+
def parse_relationship(document, xmlcontent, rel_type):
475475
"""Parse relationship document.
476476
477477
Relationships hold information like external or internal references for links.
@@ -487,7 +487,7 @@ def parse_relationship(document, xmlcontent):
487487
'type': elem.attrib['Type'],
488488
'target_mode': elem.attrib.get('TargetMode', 'Internal')}
489489

490-
document.relationships[elem.attrib['Id']] = rel
490+
document.relationships[rel_type][elem.attrib['Id']] = rel
491491

492492

493493
def parse_style(document, xmlcontent):
@@ -670,9 +670,15 @@ def parse_from_file(file_object):
670670

671671
try:
672672
doc_rel_content = file_object.read_file('_rels/document.xml.rels')
673-
parse_relationship(document, doc_rel_content)
673+
parse_relationship(document, doc_rel_content, 'document')
674674
except KeyError:
675-
logger.warning('Could not read relationships.')
675+
logger.warning('Could not read document relationships.')
676+
677+
try:
678+
doc_rel_content = file_object.read_file('_rels/endnotes.xml.rels')
679+
parse_relationship(document, doc_rel_content, 'endnotes')
680+
except KeyError:
681+
logger.warning('Could not read endnotes relationships.')
676682

677683
try:
678684
comments_content = file_object.read_file('comments.xml')

ooxml/serialize.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,8 @@ def serialize_link(ctx, document, elem, root):
291291

292292
children[-1].tail = u'{}{}'.format(_text, el.value())
293293

294-
if elem.rid in document.relationships:
295-
_a.set('href', document.relationships[elem.rid].get('target', ''))
294+
if elem.rid in document.relationships[ctx.options['relationship']]:
295+
_a.set('href', document.relationships[ctx.options['relationship']][elem.rid].get('target', ''))
296296

297297
fire_hooks(ctx, document, elem, _a, ctx.get_hook('a'))
298298

@@ -308,8 +308,8 @@ def serialize_image(ctx, document, elem, root):
308308
_img = etree.SubElement(root, 'img')
309309
# make path configurable
310310

311-
if elem.rid in document.relationships:
312-
img_src = document.relationships[elem.rid].get('target', '')
311+
if elem.rid in document.relationships[ctx.options['relationship']]:
312+
img_src = document.relationships[ctx.options['relationship']][elem.rid].get('target', '')
313313
img_name, img_extension = os.path.splitext(img_src)
314314

315315
_img.set('src', 'static/{}{}'.format(elem.rid, img_extension))
@@ -1052,7 +1052,8 @@ def get_header(self, elem, style, node):
10521052
'embed_fontsize': True,
10531053
'smarttag_span': False,
10541054
'comment_span': False,
1055-
'pretty_print': True
1055+
'pretty_print': True,
1056+
'relationship': 'document'
10561057
}
10571058

10581059

0 commit comments

Comments
 (0)