Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
43 changes: 43 additions & 0 deletions packages/markitdown/tests/test_docx_math_no_text.py
Original file line number Diff line number Diff line change
@@ -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 <m:r> with formatting tags but no <m:t>)
# This simulates the math XML structure inside a DOCX document
DOCUMENT_XML_CONTENT = f"""<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main"
xmlns:m="http://schemas.openxmlformats.org/officeDocument/2006/math">
<w:body>
<w:p>
<m:oMath>
<m:r>
<m:rPr/>
</m:r>
<m:r>
<m:t>x</m:t>
</m:r>
</m:oMath>
</w:p>
</w:body>
</w:document>
"""

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", '<?xml version="1.0" encoding="UTF-8"?><Types><Default Extension="xml" ContentType="application/xml"/></Types>')

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