Skip to content

Python tool for converting files and office documents to Markdown.

fix: guard against missing oMath element in DOCX math converter (#1979) (#1995)

hanhan76107cbd135dfa2
diff --git a/packages/markitdown/src/markitdown/converter_utils/docx/pre_process.py b/packages/markitdown/src/markitdown/converter_utils/docx/pre_process.py
index 4c2111ad..ee5da4e6 100644
--- a/packages/markitdown/src/markitdown/converter_utils/docx/pre_process.py
+++ b/packages/markitdown/src/markitdown/converter_utils/docx/pre_process.py
@@ -44,6 +44,8 @@ def _convert_omath_to_latex(tag: Tag) -> str:
math_root = ET.fromstring(MATH_ROOT_TEMPLATE.format(str(tag)))
# Find the 'oMath' element within the XML document
math_element = math_root.find(OMML_NS + "oMath")
+ if math_element is None:
+ return ""
# Convert the 'oMath' element to LaTeX using the oMath2Latex function
latex = oMath2Latex(math_element).latex
return latex
diff --git a/packages/markitdown/tests/test_module_misc.py b/packages/markitdown/tests/test_module_misc.py
index a543e377..22df3aad 100644
--- a/packages/markitdown/tests/test_module_misc.py
+++ b/packages/markitdown/tests/test_module_misc.py
@@ -479,6 +479,95 @@ def test_docx_equations() -> None:
assert block_equations, "No block equations found in the document."
+def test_docx_malformed_equations() -> None:
+ """Malformed equations should not crash the converter (issue #1979)."""
+ import zipfile
+ from io import BytesIO
+
+ docx_xml = """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<w:document xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math"
+ xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
+ xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+ mc:Ignorable="w14 wp14">
+ <w:body>
+ <w:p>
+ <w:r><w:t>Normal text</w:t></w:r>
+ </w:p>
+ <m:oMathPara>
+ <m:oMath>
+ <m:r><m:t>x+1</m:t></m:r>
+ </m:oMath>
+ </m:oMathPara>
+ <w:p>
+ <w:r><w:t>After good equation</w:t></w:r>
+ </w:p>
+ <m:oMathPara>
+ <!-- oMathPara with no oMath child -->
+ </m:oMathPara>
+ <w:p>
+ <w:r><w:t>After empty oMathPara</w:t></w:r>
+ </w:p>
+ <m:oMath>
+ <!-- empty inline oMath -->
+ </m:oMath>
+ <w:p>
+ <w:r><w:t>After empty inline oMath</w:t></w:r>
+ </w:p>
+ <oMathPara>
+ <!-- oMath outside the math namespace: BeautifulSoup matches it by local
+ name, but the namespaced lookup in _convert_omath_to_latex does not
+ find it, so the conversion has nothing to work with -->
+ <oMath><r><t>y+2</t></r></oMath>
+ </oMathPara>
+ <w:p>
+ <w:r><w:t>After unnamespaced oMathPara</w:t></w:r>
+ </w:p>
+ <oMath><r><t>z+3</t></r></oMath>
+ <w:p>
+ <w:r><w:t>After unnamespaced oMath</w:t></w:r>
+ </w:p>
+ </w:body>
+</w:document>"""
+
+ buf = BytesIO()
+ with zipfile.ZipFile(buf, "w") as z:
+ z.writestr("word/document.xml", docx_xml.encode("utf-8"))
+ z.writestr(
+ "[Content_Types].xml",
+ """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Types xmlns="http://schemas.openxmlformats.org/package/2006/content-types">
+ <Default Extension="rels" ContentType="application/vnd.openxmlformats-package.relationships+xml"/>
+ <Default Extension="xml" ContentType="application/xml"/>
+ <Override PartName="/word/document.xml" ContentType="application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml"/>
+</Types>""",
+ )
+ z.writestr(
+ "word/_rels/document.xml.rels",
+ """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+</Relationships>""",
+ )
+ z.writestr(
+ "_rels/.rels",
+ """<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
+<Relationships xmlns="http://schemas.openxmlformats.org/package/2006/relationships">
+ <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument" Target="word/document.xml"/>
+</Relationships>""",
+ )
+
+ buf.seek(0)
+ markitdown = MarkItDown()
+ result = markitdown.convert(buf)
+ assert "Normal text" in result.markdown
+ # A malformed equation must not abort the math pre-processing step, which
+ # would silently drop the LaTeX conversion of every other equation too
+ assert "$x+1$" in result.markdown or "$$x+1$$" in result.markdown
+ assert "After empty oMathPara" in result.markdown
+ assert "After empty inline oMath" in result.markdown
+ assert "After unnamespaced oMathPara" in result.markdown
+ assert "After unnamespaced oMath" in result.markdown
+
+
def test_xlsx_legacy_show_zeroes_sheetview(tmp_path) -> None:
from openpyxl import Workbook