Skip to main content

Convert AI-generated Markdown textbooks to polished DOCX with native math equations and syntax-highlighted code

Project description

markdocx

A Markdown-to-Word converter built for AI-generated textbooks

Convert Markdown files — complete with LaTeX math, syntax-highlighted code, tables, and images — into polished .docx documents in one command.

PyPI Python 3.10+ License: MIT


Why This Exists

Large language models (ChatGPT, Claude, Gemini, …) produce great Markdown, but the journey from .md to a well-formatted Word document is painful:

  • LaTeX formulas become plain text or broken images
  • Code blocks lose their highlighting
  • Tables, lists, and blockquotes need manual reformatting

markdocx bridges that gap. Feed it a Markdown file that follows a few simple rules and get a publication-ready .docx — math rendered as native Word OMML equations, code with VS Code–style colors, and everything else properly formatted.

Features

Category What you get
Math Inline ($...$) and display ($$...$$) LaTeX → native OMML equations in Word
Code 30+ languages with Pygments syntax highlighting, VS Code light theme, language labels
Tables Auto-formatted Table Grid — bold header row, left/center/right alignment, inline math in cells
Lists Bullet (•◦▪) and numbered lists, up to 6 nesting levels
Other Blockquotes, horizontal rules, clickable hyperlinks, local images, footnotes

Quick Start

Installation

# Using uv (recommended)
uv add markdocx

# Or using pip
pip install markdocx
Install from source (for development)
git clone https://github.com/shynerri-source/markdocx.git
cd markdocx
uv sync        # or: pip install -e .

Usage

CLI

# Convert a single file
markdocx input.md
markdocx input.md -o output.docx

# Convert an entire directory
markdocx ./chapters/ -o ./output/

# Recursively search subdirectories
markdocx ./chapters/ -o ./output/ -r

# Verbose logging
markdocx input.md -v

Python API

from markdocx import convert_file, convert_directory

# Single file
convert_file("input.md", "output.docx")

# Entire directory
results = convert_directory("./chapters/", output_dir="./output/", recursive=True)

CLI Options

Flag Description
input Markdown file or directory to convert
-o, --output Output file or directory path
-r, --recursive Recursively find .md files in subdirectories
-v, --verbose Show detailed processing logs

How It Works

Markdown file
     │
     ▼
 md_parser.py        ─── markdown-it-py tokenizer
     │
     ▼
 docx_builder.py     ─── walks the token stream, builds Word elements
     ├── math_renderer.py   ─── LaTeX → MathML → OMML (native Word equations)
     ├── code_renderer.py   ─── Pygments lexer → colored Word runs
     └── styles.py          ─── fonts, colors, spacing presets
     │
     ▼
  .docx file          ─── python-docx output

Math Pipeline

LaTeX is converted to native OMML (Office Math Markup Language), not images. This means formulas are editable, scale perfectly, and look like they were typed in Word's equation editor.

LaTeX string → latex2mathml → MathML → XSLT → OMML → Word paragraph

Code Pipeline

Source code → Pygments lexer + VS Code theme → colored Word runs inside a shaded table cell

Project Structure

markdocx/
├── main.py                  # Convenience entry point
├── pyproject.toml           # Project metadata & dependencies
├── src/
│   └── markdocx/            # Installable package
│       ├── __init__.py      # Public API (convert_file, convert_directory)
│       ├── cli.py           # CLI entry point (markdocx command)
│       ├── core.py          # Top-level orchestrator
│       ├── md_parser.py     # Markdown → token stream
│       ├── math_renderer.py # LaTeX → OMML (native Word math)
│       ├── code_renderer.py # Code → syntax-highlighted Word runs
│       ├── docx_builder.py  # Token stream → DOCX elements
│       └── styles.py        # Fonts, colors, and layout presets
└── rule/
    ├── ai_gen_doc_rule.md      # AI writing rules (Vietnamese)
    └── ai_gen_doc_rule_en.md   # AI writing rules (English)

Dependencies

Package Version Role
python-docx 1.2.0 DOCX generation
markdown-it-py 4.0.0 Markdown parsing
mdit-py-plugins 0.5.0 Math & footnote plugins
latex2mathml 3.78.1 LaTeX → MathML conversion
lxml 6.0.2 XML/XSLT processing
Pygments 2.19.2 Syntax highlighting
matplotlib 3.10.8 LaTeX rendering (fallback)
Pillow 12.1.0 Image processing

AI Writing Rules

The rule/ directory contains detailed guidelines for prompting AI models to produce Markdown that converts cleanly:

File Language Description
rule/ai_gen_doc_rule.md Vietnamese Full rule set — heading structure, LaTeX constraints, code block format, tables, etc.
rule/ai_gen_doc_rule_en.md English Same rules, English version

How to use: Paste the contents of the appropriate rule file into your AI system prompt (or at the start of the conversation) before asking it to write textbook content.

Contributing

Contributions are welcome. Please open an issue first to discuss what you'd like to change.

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License — see the LICENSE file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

markdocx-2.0.0.tar.gz (48.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

markdocx-2.0.0-py3-none-any.whl (22.6 kB view details)

Uploaded Python 3

File details

Details for the file markdocx-2.0.0.tar.gz.

File metadata

  • Download URL: markdocx-2.0.0.tar.gz
  • Upload date:
  • Size: 48.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for markdocx-2.0.0.tar.gz
Algorithm Hash digest
SHA256 4085f9bc3b91d02cd6fbc5cea5299161ec34b97eab014612429611d8c26fe5c1
MD5 2a8fd21800e10502bbc80f24cf61c656
BLAKE2b-256 a3266be9d7d6ffe76c925773c64d3bac790cb0837c773075a8effb2761799465

See more details on using hashes here.

File details

Details for the file markdocx-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: markdocx-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 22.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.0 {"installer":{"name":"uv","version":"0.10.0","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for markdocx-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a6e7f9aaf9d401a2b559c17cdcdd3232047bf616259747e7368c396464767e5
MD5 b2ac08123417f80dc02012b9b2e09ce2
BLAKE2b-256 06de1713e64bc9c87e913a7d4d7d6b3ee4f18e1bbbf62a2b3778809a84c78f5f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page