Skip to content

bug: DOCX math converter crashes (and silently drops all equations) when a math run has no text child #2188

Description

@S1MS4

Description

The OMML -> LaTeX converter crashes with TypeError: 'NoneType' object is not iterable when an <m:r> (math run) element has no <m:t> text child — e.g. a run that only carries formatting properties (<m:rPr/>), which some Word equation editors (and equations pasted/translated via third-party tools) produce.

Root cause

do_r() in omml.py:

def do_r(self, elm):
    _str = []
    for s in elm.findtext("./{0}t".format(OMML_NS)):
        _str.append(self._t_dict.get(s, s))
    return escape_latex(BLANK.join(_str))

elm.findtext(...) returns None when the run has no <m:t> child, and iterating over None raises TypeError.

Impact

This is more severe than a single broken equation: pre_process_docx() applies math pre-processing to the entire word/document.xml in one pass, wrapped in a blanket try/except that falls back to the original (unconverted) XML on any exception. So one malformed run anywhere in the document silently drops every native Word equation in the file — the docx still converts, but with zero equations rendered, and no error surfaced to the caller.

Reproduced with a real-world .docx containing 62 <m:oMath> elements: all 62 were dropped because a single subscript expression contained an <m:r> with no text.

Proposed fix

for s in elm.findtext("./{0}t".format(OMML_NS)) or "":
    ...

This is a distinct root cause from #1979 (which is about math_root.find("oMath") returning None in _convert_omath_to_latex) and #1982 (unsupported function names) — different function, different failure mode.

I have a fix + regression tests ready and will open a PR referencing this issue.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions