From ed6b3a42bd63774fdf018d65989cb50c33fa2ee5 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Tue, 11 May 2021 22:32:40 +0100 Subject: Handle w:num with invalid w:abstractNumId --- NEWS | 4 ++++ mammoth/docx/numbering_xml.py | 6 ++++-- tests/docx/numbering_xml_tests.py | 13 +++++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index bebc06c..5c77c48 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,7 @@ +# 1.4.16 + +* Handle w:num with invalid w:abstractNumId. + # 1.4.15 * Convert symbols in supported fonts to corresponding Unicode characters. diff --git a/mammoth/docx/numbering_xml.py b/mammoth/docx/numbering_xml.py index e1ea0b4..7f7bf0e 100644 --- a/mammoth/docx/numbering_xml.py +++ b/mammoth/docx/numbering_xml.py @@ -91,8 +91,10 @@ class Numbering(object): if num is None: return None else: - abstract_num = self._abstract_nums[num.abstract_num_id] - if abstract_num.num_style_link is None: + abstract_num = self._abstract_nums.get(num.abstract_num_id) + if abstract_num is None: + return None + elif abstract_num.num_style_link is None: return self._to_numbering_level(abstract_num.levels.get(level)) else: style = self._styles.find_numbering_style_by_id(abstract_num.num_style_link) diff --git a/tests/docx/numbering_xml_tests.py b/tests/docx/numbering_xml_tests.py index 812610e..6c1d843 100644 --- a/tests/docx/numbering_xml_tests.py +++ b/tests/docx/numbering_xml_tests.py @@ -50,6 +50,19 @@ def find_level_returns_none_if_level_cannot_be_found(): assert_equal(None, numbering.find_level("47", "2")) +@istest +def num_referencing_non_existent_abstract_num_is_ignored(): + element = xml_element("w:numbering", {}, [ + xml_element("w:num", {"w:numId": "47"}, [ + xml_element("w:abstractNumId", {"w:val": "42"}) + ]) + ]) + + numbering = _read_numbering_xml_element(element) + + assert_equal(None, numbering.find_level("47", "0")) + + @istest def when_abstract_num_has_num_style_link_then_style_is_used_to_find_num(): numbering = _read_numbering_xml_element( -- cgit v1.2.3