From 9892cf6c3fc0e3ef2a4095a742c8e8fb73113fb4 Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Thu, 13 Jun 2024 12:58:12 +0100 Subject: Handle highlight value of none --- mammoth/docx/body_xml.py | 8 +++++++- tests/docx/body_xml_tests.py | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 83cbd90..e1e70a5 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -102,7 +102,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f is_strikethrough = read_boolean_element(properties.find_child("w:strike")) is_all_caps = read_boolean_element(properties.find_child("w:caps")) is_small_caps = read_boolean_element(properties.find_child("w:smallCaps")) - highlight = properties.find_child_or_null("w:highlight").attributes.get("w:val") + highlight = read_highlight_value(properties.find_child_or_null("w:highlight").attributes.get("w:val")) def add_complex_field_hyperlink(children): hyperlink_kwargs = current_hyperlink_kwargs() @@ -139,6 +139,12 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f def read_underline_element(element): return element and element.attributes.get("w:val") not in [None, "false", "0", "none"] + def read_highlight_value(value): + if not value or value == "none": + return None + else: + return value + def paragraph(element): properties = element.find_child_or_null("w:pPr") diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index 18cba72..117d966 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -414,6 +414,11 @@ class RunTests(object): run = self._read_run_with_properties([highlight_xml]) assert_equal("yellow", run.highlight) + def test_when_highlight_is_none_then_run_has_no_highlight(self): + highlight_xml = xml_element("w:highlight", {"w:val": "none"}) + run = self._read_run_with_properties([highlight_xml]) + assert_equal(None, run.highlight) + def _read_run_with_properties(self, properties, styles=None): properties_xml = xml_element("w:rPr", {}, properties) run_xml = xml_element("w:r", {}, [properties_xml]) -- cgit v1.2.3