From bc62c8382815d5ca3b0812247cbc46ba8430ebab Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Sun, 1 Dec 2013 13:34:41 +0000 Subject: Read bold property of runs --- mammoth/documents.py | 6 +++--- mammoth/docx/document_xml.py | 8 +++++++- tests/docx/document_xml_tests.py | 19 ++++++++++++++++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/mammoth/documents.py b/mammoth/documents.py index 215df8e..03975b2 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -2,7 +2,7 @@ import collections Document = collections.namedtuple("Document", ["children"]) Paragraph = collections.namedtuple("Paragraph", ["children", "style_name", "numbering"]) -Run = collections.namedtuple("Run", ["children", "style_name"]) +Run = collections.namedtuple("Run", ["children", "style_name", "is_bold"]) Text = collections.namedtuple("Text", ["value"]) @@ -11,7 +11,7 @@ document = Document def paragraph(children, style_name=None, numbering=None): return Paragraph(children, style_name, numbering) -def run(children, style_name=None): - return Run(children, style_name) +def run(children, style_name=None, is_bold=None): + return Run(children, style_name, bool(is_bold)) text = Text diff --git a/mammoth/docx/document_xml.py b/mammoth/docx/document_xml.py index c3fb1b7..6488a25 100644 --- a/mammoth/docx/document_xml.py +++ b/mammoth/docx/document_xml.py @@ -26,7 +26,13 @@ def _create_reader(numbering): style_name = properties \ .find_child_or_null("w:rStyle") \ .attributes.get("w:val") - return documents.run(_read_xml_elements(element.children), style_name) + is_bold = properties.find_child("w:b") + + return documents.run( + children=_read_xml_elements(element.children), + style_name=style_name, + is_bold=is_bold, + ) @handler("w:p") diff --git a/tests/docx/document_xml_tests.py b/tests/docx/document_xml_tests.py index d2ec1ee..4722a4c 100644 --- a/tests/docx/document_xml_tests.py +++ b/tests/docx/document_xml_tests.py @@ -78,11 +78,24 @@ class ReadXmlElementTests(object): @istest def run_has_style_name_read_from_run_properties_if_present(self): style_xml = xml_element("w:rStyle", {"w:val": "Emphasis"}) - properties_xml = xml_element("w:rPr", {}, [style_xml]) - run_xml = xml_element("w:r", {}, [properties_xml]) - run = read_document_xml_element(run_xml) + run = self._read_run_with_properties([style_xml]) assert_equal("Emphasis", run.style_name) + @istest + def run_is_not_bold_if_bold_element_is_not_present(self): + run = self._read_run_with_properties([]) + assert_equal(False, run.is_bold) + + @istest + def run_is_bold_if_bold_element_is_present(self): + run = self._read_run_with_properties([xml_element("w:b")]) + assert_equal(True, run.is_bold) + + def _read_run_with_properties(self, properties): + properties_xml = xml_element("w:rPr", {}, properties) + run_xml = xml_element("w:r", {}, [properties_xml]) + return read_document_xml_element(run_xml) + @istest def unrecognised_elements_are_ignored(self): -- cgit v1.2.3