From 7c587440f0b3d073a6298490926f79129a650a0b Mon Sep 17 00:00:00 2001 From: Michael Williamson Date: Mon, 30 Dec 2024 10:14:55 +0000 Subject: Convert SDT checkboxes to checkbox inputs --- mammoth/docx/body_xml.py | 17 +++++++++++++++-- mammoth/docx/office_xml.py | 4 ++++ 2 files changed, 19 insertions(+), 2 deletions(-) (limited to 'mammoth') diff --git a/mammoth/docx/body_xml.py b/mammoth/docx/body_xml.py index 134917f..cc4e9ad 100644 --- a/mammoth/docx/body_xml.py +++ b/mammoth/docx/body_xml.py @@ -137,7 +137,10 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f if element is None: return False else: - return element.attributes.get("w:val") not in ["false", "0"] + return read_boolean_attribute_value(element.attributes.get("w:val")) + + def read_boolean_attribute_value(value): + return value not in ["false", "0"] def read_underline_element(element): return element and element.attributes.get("w:val") not in [None, "false", "0", "none"] @@ -569,7 +572,17 @@ def _create_reader(numbering, content_types, relationships, styles, docx_file, f return read_child_elements(element.find_child("mc:Fallback")) def read_sdt(element): - return read_child_elements(element.find_child_or_null("w:sdtContent")) + checkbox = element.find_child_or_null("w:sdtPr").find_child("wordml:checkbox") + + if checkbox is not None: + checked_element = checkbox.find_child("wordml:checked") + is_checked = ( + checked_element is not None and + read_boolean_attribute_value(checked_element.attributes.get("wordml:val")) + ) + return _success(documents.checkbox(checked=is_checked)) + else: + return read_child_elements(element.find_child_or_null("w:sdtContent")) handlers = { "w:t": text, diff --git a/mammoth/docx/office_xml.py b/mammoth/docx/office_xml.py index bebd04c..a988894 100644 --- a/mammoth/docx/office_xml.py +++ b/mammoth/docx/office_xml.py @@ -23,6 +23,10 @@ _namespaces = [ ("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006"), ("v", "urn:schemas-microsoft-com:vml"), ("office-word", "urn:schemas-microsoft-com:office:word"), + + # [MS-DOCX]: Word Extensions to the Office Open XML (.docx) File Format + # https://learn.microsoft.com/en-us/openspecs/office_standards/ms-docx/b839fe1f-e1ca-4fa6-8c26-5954d0abbccd + ("wordml", "http://schemas.microsoft.com/office/word/2010/wordml"), ] -- cgit v1.2.3