diff options
| author | Michael Williamson <mike@zwobble.org> | 2017-04-03 10:21:20 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2017-04-03 10:21:20 +0100 |
| commit | ea7996086d5f67913f4a96ce0173593e936d03b8 (patch) | |
| tree | 1bd2ce1c899022d5adb390adf38a4a938b8a8bfd | |
| parent | 3bb202f320d0fc548bbe569f3d6abdfc0698773e (diff) | |
Read explicit font for runs
| -rw-r--r-- | mammoth/documents.py | 14 | ||||
| -rw-r--r-- | mammoth/docx/body_xml.py | 2 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 11 |
3 files changed, 26 insertions, 1 deletions
diff --git a/mammoth/documents.py b/mammoth/documents.py index 327380d..a8c3fb2 100644 --- a/mammoth/documents.py +++ b/mammoth/documents.py @@ -29,6 +29,7 @@ class Run(HasChildren): is_underline = cobble.field() is_strikethrough = cobble.field() vertical_alignment = cobble.field() + font = cobble.field() @cobble.data class Text(Element): @@ -84,12 +85,23 @@ def document(children, notes=None, comments=None): def paragraph(children, style_id=None, style_name=None, numbering=None): return Paragraph(children, style_id, style_name, numbering) -def run(children, style_id=None, style_name=None, is_bold=None, is_italic=None, is_underline=None, is_strikethrough=None, vertical_alignment=None): +def run( + children, + style_id=None, + style_name=None, + is_bold=None, + is_italic=None, + is_underline=None, + is_strikethrough=None, + vertical_alignment=None, + font=None, +): if vertical_alignment is None: vertical_alignment = VerticalAlignment.baseline return Run( children, style_id, style_name, bool(is_bold), bool(is_italic), bool(is_underline), bool(is_strikethrough), vertical_alignment, + font=font, ) class VerticalAlignment(object): diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 64f83d6..76510ef 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -73,6 +73,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f vertical_alignment = properties \ .find_child_or_null("w:vertAlign") \ .attributes.get("w:val") + font = properties.find_child_or_null("w:rFonts").attributes.get("w:ascii") is_bold = read_boolean_element(properties.find_child("w:b")) is_italic = read_boolean_element(properties.find_child("w:i")) @@ -98,6 +99,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f is_underline=is_underline, is_strikethrough=is_strikethrough, vertical_alignment=vertical_alignment, + font=font, )) def _read_run_style(properties): diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index f10bee4..51d80e9 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -230,6 +230,17 @@ class RunTests(object): def run_has_vertical_alignment_read_from_vertical_alignment_element(self): run = self._read_run_with_properties([xml_element("w:vertAlign", {"w:val": "superscript"})]) assert_equal(documents.VerticalAlignment.superscript, run.vertical_alignment) + + @istest + def run_has_none_font_by_default(self): + run = self._read_run_with_properties([]) + assert_equal(None, run.font) + + @istest + def run_has_font_read_from_properties(self): + font_xml = xml_element("w:rFonts", {"w:ascii": "Arial"}) + run = self._read_run_with_properties([font_xml]) + assert_equal("Arial", run.font) def _read_run_with_properties(self, properties, styles=None): properties_xml = xml_element("w:rPr", {}, properties) |
