Skip to content

AI turns documents or topics into real, native PowerPoint decks—with native shapes, transitions and animations, data-backed charts and tables on demand, audio narration from speaker notes, and support for your own .pptx templates. · by Hugo He

feat(doc_to_md): convert OMML/Office Math equations to inline LaTeX

hugohe345d1def543ed
diff --git a/skills/ppt-master/scripts/docs/conversion.md b/skills/ppt-master/scripts/docs/conversion.md
index e795158a..acadc31f 100644
--- a/skills/ppt-master/scripts/docs/conversion.md
+++ b/skills/ppt-master/scripts/docs/conversion.md
@@ -41,7 +41,7 @@ pip install PyMuPDF
Hybrid converter: pure-Python for the common formats, pandoc fallback for the rest.
Native path (no external binary required):
-- `.docx` — via `mammoth`
+- `.docx` — via `mammoth`; OMML / Office Math equations (Word-native or MathType "Convert to Office Math") are rewritten to inline LaTeX. Classic MathType OLE objects carry no OMML and are kept only as their preview image.
- `.html` / `.htm` — via `markdownify` + `beautifulsoup4`
- `.epub` — via `ebooklib` + `markdownify`
- `.ipynb` — via `nbconvert`
diff --git a/skills/ppt-master/scripts/source_to_md/doc_to_md.py b/skills/ppt-master/scripts/source_to_md/doc_to_md.py
index 36c9c356..a36b14af 100644
--- a/skills/ppt-master/scripts/source_to_md/doc_to_md.py
+++ b/skills/ppt-master/scripts/source_to_md/doc_to_md.py
@@ -3,7 +3,7 @@
Document to Markdown Converter (hybrid Python + Pandoc fallback)
Primary formats (pure Python, no external tools required):
- .docx → mammoth
+ .docx → mammoth (OMML/Office Math equations rewritten to inline LaTeX)
.html → markdownify + BeautifulSoup
.epub → ebooklib + markdownify
.ipynb → nbconvert
@@ -27,6 +27,7 @@ import shutil
import subprocess
import sys
import tempfile
+import uuid
import zipfile
from pathlib import Path
from urllib.parse import unquote, urlparse
@@ -64,8 +65,24 @@ DOCX_NS = {
"v": "urn:schemas-microsoft-com:vml",
"o": "urn:schemas-microsoft-com:office:office",
"mc": "http://schemas.openxmlformats.org/markup-compatibility/2006",
+ "m": "http://schemas.openxmlformats.org/officeDocument/2006/math",
}
EMU_PER_INCH = 914400
+MATH_NS = DOCX_NS["m"]
+W_NS = DOCX_NS["w"]
+XML_SPACE_ATTR = "{http://www.w3.org/XML/1998/namespace}space"
+
+# OMML n-ary operator chars (m:nary/m:naryPr/m:chr) → LaTeX command.
+NARY_OPS = {
+ "∑": r"\sum", "∏": r"\prod", "∐": r"\coprod",
+ "∫": r"\int", "∬": r"\iint", "∭": r"\iiint", "∮": r"\oint",
+ "⋃": r"\bigcup", "⋂": r"\bigcap", "⋁": r"\bigvee", "⋀": r"\bigwedge",
+}
+# OMML accent chars (m:acc/m:accPr/m:chr) → LaTeX command.
+ACCENT_CMDS = {
+ "̂": r"\hat", "̃": r"\tilde", "̄": r"\bar", "→": r"\vec", "⃗": r"\vec",
+ "̇": r"\dot", "̈": r"\ddot", "̌": r"\check", "́": r"\acute", "̀": r"\grave",
+}
# ─────────────────────────────────────────────────────────────
@@ -378,6 +395,226 @@ def _manifest_entry(
return entry
+# ─────────────────────────────────────────────────────────────
+# OMML (Office Math) → LaTeX
+# ─────────────────────────────────────────────────────────────
+#
+# mammoth drops all math content, so Word-native equations and MathType
+# formulas saved as Office Math (OMML) vanish from the output. This pure-Python
+# converter rewrites each <m:oMath> into inline `$...$` LaTeX before mammoth
+# runs, so formulas survive into the Markdown in document order.
+#
+# Scope: OMML only. Classic MathType OLE objects (Equation.DSMT4 / MTEF binary)
+# carry no OMML — they expose only a WMF/EMF preview image, which mammoth still
+# emits as a picture. Decoding MTEF is out of scope.
+
+def _m_child(elem: ET.Element, name: str) -> ET.Element | None:
+ """Return the first OMML child with the given local name."""
+ for child in elem:
+ if _local_name(child) == name:
+ return child
+ return None
+
+
+def _m_pr_val(elem: ET.Element, prop: str) -> str | None:
+ """Return m:val of a property inside the element's *Pr block (e.g. chr)."""
+ for child in elem:
+ if not _local_name(child).endswith("Pr"):
+ continue
+ for sub in child:
+ if _local_name(sub) == prop:
+ return sub.get(f"{{{MATH_NS}}}val")
+ return None
+
+
+def _brace(latex: str) -> str:
+ """Wrap multi-char LaTeX in braces so it binds as one super/subscript arg."""
+ return latex if len(latex) <= 1 else "{" + latex + "}"
+
+
+def _omml_part(elem: ET.Element, name: str) -> str:
+ """Convert a named OMML child (e/num/den/sup/sub/...) to LaTeX."""
+ child = _m_child(elem, name)
+ return _omml_to_latex(child) if child is not None else ""
+
+
+def _omml_run(elem: ET.Element) -> str:
+ """Concatenate text from an OMML run, skipping property children."""
+ return "".join(
+ c.text or "" for c in elem if _local_name(c) == "t"
+ )
+
+
+def _omml_children(elem: ET.Element) -> str:
+ """Convert all non-property children in order (default/passthrough rule)."""
+ return "".join(
+ _omml_to_latex(c) for c in elem if not _local_name(c).endswith("Pr")
+ )
+
+
+def _omml_matrix(elem: ET.Element, *, environment: str) -> str:
+ """Convert a matrix (m:m) or equation array (m:eqArr) to a LaTeX env."""
+ rows: list[str] = []
+ for row in elem:
+ if _local_name(row) not in ("mr", "e"):
+ continue
+ if _local_name(row) == "e": # eqArr stores rows as bare <m:e>
+ rows.append(_omml_to_latex(row))
+ continue
+ cells = [_omml_to_latex(cell) for cell in row if _local_name(cell) == "e"]
+ rows.append(" & ".join(cells))
+ body = r" \\ ".join(rows)
+ return rf"\begin{{{environment}}} {body} \end{{{environment}}}"
+
+
+def _omml_to_latex(elem: ET.Element) -> str:
+ """Recursively convert one OMML element subtree to a LaTeX string.
+
+ Unknown elements degrade to a concatenation of their children rather than
+ being dropped, so rare constructs lose markup but never lose content.
+ """
+ local = _local_name(elem)
+
+ if local == "t":
+ return elem.text or ""
+ if local == "r":
+ return _omml_run(elem)
+ if local in ("oMath", "oMathPara", "e", "num", "den", "sup", "sub",
+ "deg", "fName", "lim", "box", "borderBox"):
+ return _omml_children(elem)
+ if local == "sSup":
+ return _brace(_omml_part(elem, "e")) + "^" + _brace(_omml_part(elem, "sup"))
+ if local == "sSub":
+ return _brace(_omml_part(elem, "e")) + "_" + _brace(_omml_part(elem, "sub"))
+ if local == "sSubSup":
+ return (_brace(_omml_part(elem, "e"))
+ + "_" + _brace(_omml_part(elem, "sub"))
+ + "^" + _brace(_omml_part(elem, "sup")))
+ if local == "sPre":
+ return ("{}_" + _brace(_omml_part(elem, "sub"))
+ + "^" + _brace(_omml_part(elem, "sup"))
+ + _brace(_omml_part(elem, "e")))
+ if local == "f":
+ return r"\frac{" + _omml_part(elem, "num") + "}{" + _omml_part(elem, "den") + "}"
+ if local == "rad":
+ deg = _m_child(elem, "deg")
+ body = _omml_part(elem, "e")
+ deg_latex = _omml_to_latex(deg) if deg is not None and len(deg) else ""
+ return rf"\sqrt[{deg_latex}]{{{body}}}" if deg_latex else rf"\sqrt{{{body}}}"
+ if local == "d":
+ beg = _m_pr_val(elem, "begChr")
+ end = _m_pr_val(elem, "endChr")
+ beg = "(" if beg is None else (beg or ".")
+ end = ")" if end is None else (end or ".")
+ inner = "".join(_omml_to_latex(c) for c in elem if _local_name(c) == "e")
+ return rf"\left{beg}{inner}\right{end}"
+ if local == "nary":
+ chr_ = _m_pr_val(elem, "chr") or "∫"
+ op = NARY_OPS.get(chr_, chr_)
+ sub, sup = _m_child(elem, "sub"), _m_child(elem, "sup")
+ out = op
+ if sub is not None and len(sub):
+ out += "_" + _brace(_omml_to_latex(sub))
+ if sup is not None and len(sup):
+ out += "^" + _brace(_omml_to_latex(sup))
+ return out + _brace(_omml_part(elem, "e"))
+ if local == "func":
+ return "\\" + _omml_part(elem, "fName").strip() + _brace(_omml_part(elem, "e"))
+ if local == "limLow":
+ return _brace(_omml_part(elem, "e")) + "_" + _brace(_omml_part(elem, "lim"))
+ if local == "limUpp":
+ return _brace(_omml_part(elem, "e")) + "^" + _brace(_omml_part(elem, "lim"))
+ if local == "bar":
+ cmd = r"\underline" if _m_pr_val(elem, "pos") == "bot" else r"\overline"
+ return cmd + "{" + _omml_part(elem, "e") + "}"
+ if local == "acc":
+ cmd = ACCENT_CMDS.get(_m_pr_val(elem, "chr") or "̂", r"\hat")
+ return cmd + "{" + _omml_part(elem, "e") + "}"
+ if local == "groupChr":
+ return _omml_part(elem, "e")
+ if local == "m":
+ return _omml_matrix(elem, environment="matrix")
+ if local == "eqArr":
+ return _omml_matrix(elem, environment="aligned")
+
+ return _omml_children(elem)
+
+
+def _make_text_run(text: str) -> ET.Element:
+ """Build a <w:r><w:t xml:space="preserve">text</w:t></w:r> element."""
+ run = ET.Element(f"{{{W_NS}}}r")
+ t = ET.SubElement(run, f"{{{W_NS}}}t")
+ t.set(XML_SPACE_ATTR, "preserve")
+ t.text = text
+ return run
+
+
+def _docx_inject_math_latex(
+ input_file: Path,
+) -> tuple[Path, dict[str, str]] | None:
+ """Replace OMML equations with alphanumeric placeholders in a temp DOCX.
+
+ Returns ``(temp_file, {placeholder: latex})`` or None when the document has
+ no OMML math. Placeholders are plain ``[A-Za-z0-9]`` tokens so mammoth never
+ markdown-escapes the LaTeX; the caller swaps each token for its `$...$` value
+ after mammoth has produced the Markdown.
+ """
+ try:
+ with zipfile.ZipFile(input_file) as docx:
+ document_xml = docx.read("word/document.xml")
+ except (KeyError, zipfile.BadZipFile, OSError):
+ return None
+ try:
+ root = ET.fromstring(document_xml)
+ except ET.ParseError:
+ return None
+
+ parent_map = {child: parent for parent in root.iter() for child in parent}
+ targets: list[tuple[ET.Element, bool]] = []
+ for elem in root.iter():
+ local = _local_name(elem)
+ if local == "oMathPara":
+ targets.append((elem, True))
+ elif local == "oMath":
+ parent = parent_map.get(elem)
+ if parent is None or _local_name(parent) != "oMathPara":
+ targets.append((elem, False))
+ if not targets:
+ return None
+
+ token_base = uuid.uuid4().hex
+ replacements: dict[str, str] = {}
+ for index, (elem, display) in enumerate(targets):
+ parent = parent_map.get(elem)
+ if parent is None:
+ continue
+ latex = _omml_to_latex(elem).strip()
+ position = list(parent).index(elem)
+ parent.remove(elem)
+ if latex:
+ token = f"MATHEQ{token_base}{index:04d}"
+ delim = "$$" if display else "$"
+ replacements[token] = f"{delim}{latex}{delim}"
+ parent.insert(position, _make_text_run(token))
+ if not replacements:
+ return None
+
+ for prefix, uri in DOCX_NS.items():
+ if prefix != "rel":
+ ET.register_namespace(prefix, uri)
+ patched_xml = ET.tostring(root, encoding="utf-8", xml_declaration=True)
+
+ tmp = tempfile.NamedTemporaryFile(suffix=".docx", delete=False)
+ tmp.close()
+ out_path = Path(tmp.name)
+ with zipfile.ZipFile(input_file) as zin, \
+ zipfile.ZipFile(out_path, "w", zipfile.ZIP_DEFLATED) as zout:
+ for item in zin.infolist():
+ data = patched_xml if item.filename == "word/document.xml" else zin.read(item.filename)
+ zout.writestr(item, data)
+ return out_path, replacements
+
+
# ─────────────────────────────────────────────────────────────
# DOCX → Markdown (mammoth)
# ─────────────────────────────────────────────────────────────
@@ -434,13 +671,31 @@ def _convert_docx(input_file: Path, out_file: Path) -> str:
))
return {"src": f"{rel_media_dir}/{filename}"}
- with input_file.open("rb") as f:
- result = mammoth.convert_to_markdown(
- f,
- convert_image=mammoth.images.img_element(_save_image),
- )
+ # Rewrite OMML equations to LaTeX placeholders before mammoth (which would
+ # otherwise drop them); the placeholders are swapped back below.
+ math_injection = _docx_inject_math_latex(input_file)
+ if math_injection is not None:
+ math_file, math_replacements = math_injection
+ else:
+ math_file, math_replacements = None, {}
+ mammoth_source = math_file or input_file
+ try:
+ with mammoth_source.open("rb") as f:
+ result = mammoth.convert_to_markdown(
+ f,
+ convert_image=mammoth.images.img_element(_save_image),
+ )
+ finally:
+ if math_file is not None:
+ try:
+ math_file.unlink()
+ except OSError:
+ pass
- markdown = _html_img_to_md(result.value)
+ markdown = result.value
+ for token, latex in math_replacements.items():
+ markdown = markdown.replace(token, latex)
+ markdown = _html_img_to_md(markdown)
out_file.write_text(markdown, encoding="utf-8")
if manifest: