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

This README describes the 1.0 API. Install the repository version while 1.0 is unreleased on PyPI:

python -m pip install 'git+https://github.com/joongi007/mathml2docx.git@master'
# With optional LaTeX and Markdown support:
python -m pip install 'mathml2docx[latex,markdown] @ git+https://github.com/joongi007/mathml2docx.git@master'

Python 3.6.2+ is required for the optional LaTeX frontend. Library users do not need uv. For a reproducible installation, replace master with a reviewed commit or release tag.

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.0

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.0
File Size Uploaded Provenance Actions
mathml2docx-1.0.0.tar.gz 1.3 MB With provenance Details for mathml2docx-1.0.0.tar.gz

Built distribution (wheel)

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

Total release size: 1.3 MB

Release files / mathml2docx-1.0.0.tar.gz

Download URL mathml2docx-1.0.0.tar.gz
Size 1.3 MB
Tags Source
SHA-256 checksum
How to use checksums
66f740467b18daf1e773e89e3fe5509b09046c82388ee1e6e7ff0ebde2b7ff05
BLAKE2b-256 checksum
How to use checksums
575646c682a330eb6134065bd80c67d1bcf9aca5d27cba6a9095328bc5934029
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 12, 2026.

Transparency log

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

Download URL mathml2docx-1.0.0-py3-none-any.whl
Size 41.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7849b35456524f8180fbb93c9b54ca79d76c06879bbc6d89f5890715ca5481b5
BLAKE2b-256 checksum
How to use checksums
47f9e543a06ff9af8aa124dbf5be6cfe1c1d988f85322a6959347b9b44c091b2
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 12, 2026.

Transparency log

Security for mathml2docx 1.0.0

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.0.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 12, 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