diff --git a/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py b/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py index dfa734cdc..3e1a6fdbd 100644 --- a/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py +++ b/packages/markitdown/src/markitdown/converter_utils/docx/math/omml.py @@ -373,7 +373,8 @@ def do_r(self, elm): @todo \text (latex pure text support) """ _str = [] - for s in elm.findtext("./{0}t".format(OMML_NS)): + text_content = elm.findtext("./{0}t".format(OMML_NS)) + for s in (text_content or ""): # s = s if isinstance(s,unicode) else unicode(s,'utf-8') _str.append(self._t_dict.get(s, s)) return escape_latex(BLANK.join(_str)) diff --git a/packages/markitdown/tests/test_docx_math_no_text.py b/packages/markitdown/tests/test_docx_math_no_text.py new file mode 100644 index 000000000..73f74dd26 --- /dev/null +++ b/packages/markitdown/tests/test_docx_math_no_text.py @@ -0,0 +1,43 @@ +import io +import zipfile +import pytest +from markitdown import MarkItDown +from markitdown.converter_utils.docx.math.omml import OMML_NS + +# Minimal Word Document XML structure with a math run having no text child +# (an with formatting tags but no ) +# This simulates the math XML structure inside a DOCX document +DOCUMENT_XML_CONTENT = f""" + + + + + + + + + x + + + + + +""" + +def test_math_run_no_text_child(): + # Build a minimal docx in-memory + docx_buffer = io.BytesIO() + with zipfile.ZipFile(docx_buffer, "w") as z: + # docx needs word/document.xml to be valid + z.writestr("word/document.xml", DOCUMENT_XML_CONTENT) + # minimal content types + z.writestr("[Content_Types].xml", '') + + docx_buffer.seek(0) + + # We should be able to convert this without a TypeError crash, + # and the valid math run ('x') should still be preserved/rendered. + markitdown = MarkItDown() + result = markitdown.convert_stream(docx_buffer, file_extension=".docx") + assert "$x$" in result.text_content