diff options
| author | Michael Williamson <mike@zwobble.org> | 2020-04-12 15:09:16 +0100 |
|---|---|---|
| committer | Michael Williamson <mike@zwobble.org> | 2020-04-12 15:09:16 +0100 |
| commit | 51e109968beb0c4f2fc4ac8524c3bbf1b74da369 (patch) | |
| tree | 9bb7a89c0b52c3cad074fabea2380a371a5fb253 | |
| parent | a3c287007942dbe119ebdc7111f5996e083ced7e (diff) | |
Only parse integer font sizes
| -rw-r--r-- | mammoth/docx/body_xml.py | 14 | ||||
| -rw-r--r-- | tests/docx/body_xml_tests.py | 6 |
2 files changed, 19 insertions, 1 deletions
diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 7bb8628..e65c5b2 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -81,7 +81,7 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f font = properties.find_child_or_null("w:rFonts").attributes.get("w:ascii") font_size_string = properties.find_child_or_null("w:sz").attributes.get("w:val") - if font_size_string: + if _is_int(font_size_string): # w:sz gives the font size in half points, so halve the value to get the size in points font_size = int(font_size_string) / 2 else: @@ -602,3 +602,15 @@ def _add_attrs(obj, **kwargs): setattr(obj, key, value) return obj + + +def _is_int(value): + if value is None: + return False + + try: + int(value) + except ValueError: + return False + + return True diff --git a/tests/docx/body_xml_tests.py b/tests/docx/body_xml_tests.py index 2258330..17d631d 100644 --- a/tests/docx/body_xml_tests.py +++ b/tests/docx/body_xml_tests.py @@ -336,6 +336,12 @@ class RunTests(object): run = self._read_run_with_properties([font_size_xml]) assert_equal(14, run.font_size) + @istest + def run_with_invalid_w_sz_has_none_font_size(self): + font_size_xml = xml_element("w:sz", {"w:val": "28a"}) + run = self._read_run_with_properties([font_size_xml]) + assert_equal(None, run.font_size) + def _read_run_with_properties(self, properties, styles=None): properties_xml = xml_element("w:rPr", {}, properties) run_xml = xml_element("w:r", {}, [properties_xml]) |
