Skip to main content

markdown-docx

markdown-docx turns constrained Markdown into predictable, editable Word .docx files. The Markdown stays readable in normal renderers. Word-specific layout and style settings live in invisible HTML comments.

The format is strict by design. Unsupported input produces a stable, line-aware error instead of an approximate document.

Prerequisite

The documented workflow uses uv. Install uv before continuing.

Quick start with an agent

Install the managed agent skill:

uvx markdown-docx skill install

Then ask an agent to create both the Markdown source and the editable Word output:

Use $markdown-docx to create a project brief. Keep the source readable as normal Markdown and save the editable DOCX beside it.

The skill teaches the agent how to inspect the format, inspect blank Word templates, render safely, and handle structured results.

Use the CLI directly

uvx markdown-docx document.md document.docx

When the output path is omitted, the tool writes a .docx beside the input Markdown file.

uvx markdown-docx document.md

Inspect the supported syntax:

uvx markdown-docx --syntax
uvx markdown-docx --syntax --json

Supported Markdown

Version 0.1.0 supports:

  • ATX headings from # through ######
  • Paragraphs and standard soft or hard line breaks
  • Emphasis, strong emphasis, and inline backtick code
  • Fenced code blocks
  • Blockquotes containing paragraphs
  • Ordered and unordered lists, including mixed nesting
  • Pipe tables with inline text formatting
  • Local and remote inline images
  • Standalone images with width and alignment metadata

Links are rejected in 0.1.0. python-docx 1.2.0 can read hyperlinks but has no supported public API for creating them. The source alt text for images remains meaningful Markdown content, but the same library release has no public API for embedding it in a Word drawing. A rendered document containing images reports image_alt_text_not_embedded in its warning list.

The following syntax is intentionally unsupported:

  • Raw HTML and non-reserved HTML comments
  • Setext headings and horizontal rules
  • Indented code blocks
  • Task lists and footnotes
  • Multi-paragraph list items
  • Tables, images, code blocks, or blockquotes nested inside list items
  • Images inside table cells or blockquotes
  • Arbitrary Markdown extensions

Invisible Word metadata

Only HTML comments beginning with markdown-docx are accepted. All other HTML is an error.

Document metadata may appear once. It must be the first non-whitespace content:

<!-- markdown-docx
document:
  page_size: letter
  orientation: portrait
  margins:
    top: 1in
    right: 1in
    bottom: 1in
    left: 1in
  styles:
    paragraph: Normal
    headings:
      1: Heading 1
      2: Heading 2
      3: Heading 3
      4: Heading 4
      5: Heading 5
      6: Heading 6
    blockquote: Quote
    code_block: Code Block
    ordered_list:
      - List Number
      - List Number 2
      - List Number 3
    unordered_list:
      - List Bullet
      - List Bullet 2
      - List Bullet 3
    table: Table Grid
  fonts:
    body: Calibri
    headings: Calibri
    monospace: Consolas
-->

# Project brief

This remains ordinary Markdown.

Metadata uses strict YAML. Duplicate keys, unknown keys, invalid types, unitless lengths, and misplaced comments are errors.

Page sizes and margins

Named page sizes are letter, legal, and a4. Custom page sizes use nominal portrait dimensions. The width must not exceed the height.

<!-- markdown-docx
document:
  page_size:
    width: 7in
    height: 10in
  orientation: portrait
  margins:
    top: 0.75in
    right: 0.75in
    bottom: 0.75in
    left: 0.75in
-->

Lengths accept in, cm, mm, and pt. Margins must leave a positive usable page area.

Sections

A section directive starts a next-page Word section before the following content block:

<!-- markdown-docx
section:
  page_size: letter
  orientation: landscape
  margins:
    top: 0.75in
    right: 0.75in
    bottom: 0.75in
    left: 0.75in
-->

## Landscape analysis

Each section starts from the document defaults and applies its own overrides. It does not inherit omitted values from the preceding section. Reset to the document defaults with:

<!-- markdown-docx
section: default
-->

Headings never create sections.

Page breaks

Insert a page break before the next content block:

<!-- markdown-docx: page-break -->

## Appendix

A page break does not create a new section.

Templates and styles

A custom template must be a blank .docx formatting template. .dotx is not supported. The template may define styles, theme data, fonts, numbering definitions, and section defaults. It must not contain:

  • Non-whitespace body text
  • Body tables
  • Body images or drawings
  • Nonempty headers or footers

Inspect a template before writing Markdown that refers to its style names:

uvx markdown-docx --inspect-template --template formatting.docx
uvx markdown-docx --list-styles --template formatting.docx
uvx markdown-docx --list-table-styles --template formatting.docx

Render with the template:

uvx markdown-docx report.md report.docx --template formatting.docx

The template remains unchanged. When no template is supplied, the packaged blank template provides every default style.

The Markdown metadata maps semantic constructs to paragraph and table style names. Each configured style must exist and must have the correct Word style type. Body and heading font overrides modify the mapped paragraph styles. The monospace override applies to code blocks and inline code. Sizes, colors, spacing, borders, and other typography remain owned by the template.

Lists

Apply one Word paragraph style per list type and nesting depth:

styles:
  ordered_list:
    - List Number
    - List Number 2
    - List Number 3
  unordered_list:
    - List Bullet
    - List Bullet 2
    - List Bullet 3

Mixed nested lists select ordered or unordered styles independently at every depth. A list deeper than the configured style array is an error. Ordered lists must begin with 1. Restart controls and arbitrary start values are not supported.

Tables

Standard pipe tables become editable Word tables. Put optional table metadata immediately before the table:

<!-- markdown-docx
table:
  style: Table Grid
  alignment: center
  width: page
  column_widths: [3, 1, 1]
-->

| Item | Count | Price |
| --- | ---: | ---: |
| Widget | 2 | $10 |

The first Markdown row is the semantic header row. Cell alignment follows the Markdown delimiter row. column_widths contains positive ratios and must match the column count. width: page uses the active section's usable width. width: auto lets Word size the table unless ratios are supplied.

Merged cells, nested tables, fixed row heights, repeated-header controls, and per-cell border or fill metadata are not supported.

Images

Relative paths resolve from the Markdown file's directory:

Text before ![Status icon](images/status.png) text after.

Put metadata immediately before a standalone image:

<!-- markdown-docx
image:
  width: 40%
  alignment: center
-->
![Architecture](images/architecture.png)

Widths accept in, cm, mm, pt, or a percentage of the active section's usable width. Images preserve aspect ratio. Natural-size images are clamped to the usable width. An explicit physical width that exceeds the usable width is an error.

HTTP and HTTPS images use timeouts, a 25 MiB download limit, content-type validation, a 50 megapixel decode limit, and one download per unique URL. Reject remote images for offline builds or untrusted input:

uvx markdown-docx report.md --no-remote-images

Automation and safety

Use --json for one complete machine-readable result:

uvx markdown-docx report.md report.docx --json

A successful result contains the input path, output path, template identifier, section count, and warnings. It does not report a page count. DOCX files do not have a reliable intrinsic page count until a compatible layout engine paginates them.

The CLI refuses to overwrite an existing output. Add --force only when replacement is intended:

uvx markdown-docx report.md report.docx --force --json

For stdin, provide both an output path and a base directory:

Get-Content report.md | uvx markdown-docx --input - --output report.docx --base-dir .

Exit codes:

Code Meaning
0 Success
2 Usage or input error
3 Markdown or metadata parse error
4 Template or style error
5 Image or asset error
6 Unsupported Markdown or feature
7 DOCX rendering error
8 Unexpected internal error

Complete example

See the showcase Markdown. It opens with an illustrated three-dog story, then exercises supported text, list, table, image, page-break, and section behavior in a dedicated capability lab. Regenerate it from a repository checkout:

uvx --refresh --from . markdown-docx sample\showcase.md sample\showcase.docx --force

Public API boundary

Production code uses only documented public python-docx APIs. It does not write OOXML directly and does not call private library members. Tests may inspect generated package XML read-only. The complete capability decision record is in docs/public-api-capabilities.md.

Word is the primary compatibility target. LibreOffice Writer is used as a visual smoke-test engine. Differences in pagination or font metrics can occur between layout engines.

Development

$env:UV_LINK_MODE="copy"
uv sync --locked --all-groups
uv run pytest
uv run ruff check .
uv run ruff format --check .
uv run mypy

Build and validate distributions:

uv build
uv run twine check dist/*

Download files

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

Source Distribution

markdown_docx-0.1.0.tar.gz (4.5 MB view details)

Uploaded Source

Built Distribution

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

markdown_docx-0.1.0-py3-none-any.whl (67.1 kB view details)

Uploaded Python 3

File details

Details for the file markdown_docx-0.1.0.tar.gz.

File metadata

  • Download URL: markdown_docx-0.1.0.tar.gz
  • Upload date:
  • Size: 4.5 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for markdown_docx-0.1.0.tar.gz
Algorithm Hash digest
SHA256 ff30828778176408b947b1e0e789e515a846a8b9dd818b6e761e1562f0fbf675
MD5 8fabb89dcbed8a9659de713a02e94402
BLAKE2b-256 0f3cbef3ecaae23e81c6d4972d627d5543af7f9801942e2cccfe947ce5a0d126

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_docx-0.1.0.tar.gz:

Publisher: publish-pypi.yml on pseudosavant/markdown-docx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file markdown_docx-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: markdown_docx-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 67.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for markdown_docx-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6fca408525289cb1417ba4d728a3c1c8a878d6ac30bc77b3f7e9dc3c59c6c1b8
MD5 aefbe7aed1e193381061802e5d76db94
BLAKE2b-256 f526e49c2a0bcb2fd1ad5ec7e36066c00de44cacae332e48e1bfa43f8bada518

See more details on using hashes here.

Provenance

The following attestation bundles were made for markdown_docx-0.1.0-py3-none-any.whl:

Publisher: publish-pypi.yml on pseudosavant/markdown-docx

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.0

2 files

This release

0.1.0 This release

2 files

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