Skip to main content

mathml2docx

English · 한국어 · Documentation

Convert HTML and MathML into Word documents with Python. Create reports, worksheets and templates with editable equations, formatted text, tables and images. Microsoft Office is not required to generate a document.

Python 3.6+ · Optional LaTeX and Markdown inputs · MIT license

Installation

Install from PyPI using your project's package manager:

Tool HTML / MathML With LaTeX and Markdown
pip python -m pip install mathml2docx python -m pip install "mathml2docx[latex,markdown]"
uv uv add mathml2docx uv add "mathml2docx[latex,markdown]"
Poetry poetry add mathml2docx poetry add "mathml2docx[latex,markdown]"

Use pip in your Python environment, or run add in an initialized uv/Poetry project. Run your script with python report.py, uv run python report.py, or poetry run python report.py, respectively. See installation and updates for project setup, individual extras and Python 3.6 instructions.

The library supports Python 3.6+; optional LaTeX requires 3.6.2+. Package managers have their own Python requirements. Pin dependencies in your requirements or lockfile for reproducible installations. Users upgrading from 0.x should read the migration guide. To modify the library, see development setup.

Your first Word document

from mathml2docx import convert

convert('''
<h1>Lesson notes</h1>
<p>The value <math><mfrac><mn>1</mn><mn>2</mn></mfrac></math> is one half.</p>
<math display="block"><msup><mi>x</mi><mn>2</mn></msup></math>
''', 'lesson.docx')

Open lesson.docx in Word to edit its text and equations. convert() also returns a python-docx Document; omit the output path to continue editing it in Python.

What it supports

Input or task Features
HTML Headings, paragraphs, links, nested lists and text formatting
Tables Merged cells, nested tables and Word table styles
Images Data URLs, configured local files and explicit application loaders
CSS Supported selectors, fonts, colors, alignment and page breaks
MathML Fractions, roots, scripts, limits, accents, matrices, selected enclosures and isotope scripts
LaTeX / Markdown Optional frontends using latex2mathml and Python-Markdown
Templates Add to an existing document or cell; replace markers and bookmarks

The converter implements a documented subset of HTML, CSS and Presentation MathML. It does not reproduce a complete browser layout.

Equation layout and unsupported input

<math> is inline by default, following MathML. Use display="block" for a separate display equation. To retain block behavior for input that omits display:

from mathml2docx import MathmlToDocx

parser = MathmlToDocx(default_math_display='block')

Unsupported mathematical structures raise UnsupportedMathMLError by default. Applications handling mixed input can select unsupported_math='text' to preserve the formula's XML visibly, provide mathml_fallback(source, error) returning text or an image, or normalize input with mathml_preprocessor(source). Recovery emits MathMLFallbackWarning; invalid XML still raises an error. See equation handling for supported notations and complete examples.

LaTeX, Markdown and command line

from mathml2docx import MathmlToDocx

parser = MathmlToDocx()
parser.parse_latex(r'\frac{1}{2} + x_1^2').save('equation.docx')
parser.parse_markdown('# Notes\n\n**Bold** and ~~removed~~ text.').save('notes.docx')
mathml2docx input.html output.docx
mathml2docx equation.tex equation.docx --format latex
mathml2docx input.html output.docx --default-math-display block --unsupported-math text

Use --force to replace an existing output file. HTML dollar delimiters are not interpreted as LaTeX. Network images and stylesheets require explicit loaders.

Guides and examples

For headless PDF conversion with LibreOffice, install Writer and Math, plus appropriate fonts. Writer-only installations can leave equations blank. The rendering guide includes installation and verification commands. Use math_target='libreoffice' with an explicit fallback for known enclosure differences.

Contributing and license

Work from develop; see development and releases for setup, tests and publishing. Changes are listed in CHANGELOG.md.

MIT, derived from html2docx. Dependencies retain their own licenses; see THIRD_PARTY_NOTICES.md. The 1.0 converter constructs OMML directly and includes no Office XSL. Historical 0.x distributions are covered separately in the provenance record.

Metadata

Release files for mathml2docx 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mathml2docx 1.0.2
File Size Uploaded Provenance Actions
mathml2docx-1.0.2.tar.gz 1.3 MB With provenance Details for mathml2docx-1.0.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mathml2docx 1.0.2
File Interpreter ABI Platform Actions
mathml2docx-1.0.2-py3-none-any.whl Python 3 none any Details for mathml2docx-1.0.2-py3-none-any.whl

Total release size: 1.4 MB

Release files / mathml2docx-1.0.2.tar.gz

Download URL mathml2docx-1.0.2.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
1aa9b1e5fc585ee12262ae1523bc38b98a84292578f2ea3dd17afb8961e570fc
BLAKE2b-256 checksum
How to use checksums
fba1a9fc5ed9da5c936bc758fae1585e84139a0ea2c4cc4a89e2b6af7569bcba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Release files / mathml2docx-1.0.2-py3-none-any.whl

Download URL mathml2docx-1.0.2-py3-none-any.whl
Size 42.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b3cdcceac06d8099d5af8e5ed5960263697c9ec2d4979c03e57c6d4bac646227
BLAKE2b-256 checksum
How to use checksums
cf498fd159a91ef5f46abb650636c39b49b896df36afc38ad385a093ff22ff3b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

Security for mathml2docx 1.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for mathml2docx-1.0.2.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 14, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page