Skip to main content

Convert Markdown files to Word documents with extensive customization

Project description

md2word

中文文档 | English

Convert Markdown files to Word documents (.docx) with extensive customization options.

Features

  • Convert Markdown to Word documents
  • Support for tables, code blocks, images, and more
  • Automatic download and embedding of web images
  • Automatic conversion of unsupported image formats (e.g., WebP)
  • LaTeX formula support (converted to native Word equations)
  • Configurable styles for headings, body text, and other elements
  • Chinese font size support (e.g., "四号", "小四")
  • Automatic heading numbering with multiple formats
  • Optional table of contents generation

Installation

Using uv (recommended)

# Install globally
uv tool install md2word

# Or run directly without installation
uvx md2word input.md

Using pip

pip install md2word

Usage

Command Line

# Basic conversion (outputs input.docx)
md2word input.md

# Specify output file
md2word input.md -o output.docx

# Use custom config file
md2word input.md -c my_config.json

# Add table of contents
md2word input.md --toc

# Custom TOC title and level
md2word input.md --toc --toc-title "Contents" --toc-level 4

# Generate default config file
md2word --init-config

As a Python Library

import md2word

# Simple conversion
md2word.convert_file("input.md", "output.docx")

# With custom configuration
config = md2word.Config.from_file("config.json")
md2word.convert_file("input.md", "output.docx", config=config, toc=True)

# Convert from string
markdown_content = "# Hello World\n\nThis is a test."
md2word.convert(markdown_content, "output.docx")

# Programmatic configuration
config = md2word.Config()
config.default_font = "Arial"
config.styles["heading_1"] = md2word.StyleConfig(
    font_name="Arial",
    font_size=24,
    bold=True,
    alignment="center",
    numbering_format="chapter",
)
md2word.convert_file("input.md", "output.docx", config=config)

Configuration

Create a config.json file to customize document styles.

Example Configuration

{
    "document": {
        "default_font": "Arial",
        "max_image_width_inches": 6.0
    },
    "styles": {
        "heading_1": {
            "font_name": "Arial",
            "font_size": 24,
            "bold": true,
            "alignment": "center",
            "line_spacing_rule": "exact",
            "line_spacing_value": 28,
            "numbering_format": "chapter"
        },
        "body": {
            "font_name": "Times New Roman",
            "font_size": 12,
            "alignment": "justify",
            "line_spacing_rule": "multiple",
            "line_spacing_value": 1.5,
            "first_line_indent": 2
        }
    }
}

Style Properties

Property Type Description
font_name string Font name
font_size number/string Font size (points or Chinese size name)
bold boolean Bold text
italic boolean Italic text
color string Font color (hex, e.g., "000000")
alignment string Paragraph alignment: left/center/right/justify
line_spacing_rule string Line spacing mode (see below)
line_spacing_value number Line spacing value
first_line_indent number First line indent (in characters)
left_indent float Left indent (in inches)
space_before number Space before paragraph (points)
space_after number Space after paragraph (points)
numbering_format string Heading numbering format (see below)

Line Spacing Modes

Value Description
single Single line spacing
1.5 1.5 line spacing
double Double line spacing
multiple Multiple line spacing (use line_spacing_value as multiplier)
exact Exact line spacing (use line_spacing_value in points)
at_least Minimum line spacing (use line_spacing_value in points)

Numbering Formats

Format Example
chapter 第一章, 第二章, 第三章...
section 第一节, 第二节, 第三节...
chinese 一、二、三...
chinese_paren (一)(二)(三)...
arabic 1. 2. 3...
arabic_paren (1) (2) (3)...
arabic_bracket [1] [2] [3]...
roman I. II. III...
roman_lower i. ii. iii...
letter A. B. C...
letter_lower a. b. c...
circle ① ② ③...
none No numbering

Custom format strings are also supported using {n} for Arabic numbers and {cn} for Chinese numbers.

Table Configuration

Configure table appearance in the table section:

{
    "table": {
        "border_style": "single",
        "border_color": "000000",
        "border_width": 4,
        "header_background_color": "D9E2F3",
        "cell_background_color": null,
        "alternating_row_color": "F2F2F2",
        "cell_padding_top": 2,
        "cell_padding_bottom": 2,
        "cell_padding_left": 5,
        "cell_padding_right": 5,
        "width_mode": "full",
        "width_inches": null
    }
}
Property Type Description
border_style string Border style: single/double/dotted/dashed/none
border_color string Border color (hex, e.g., "000000")
border_width number Border width in 1/8 points (4 = 0.5pt, 8 = 1pt)
header_background_color string Header row background color (hex)
cell_background_color string Cell background color (hex)
alternating_row_color string Alternating row color for zebra striping (hex)
cell_padding_* number Cell padding in points (top/bottom/left/right)
width_mode string Table width mode: auto/full/fixed
width_inches number Fixed width in inches (when width_mode is "fixed")

Chinese Font Sizes

Name Points Name Points
初号 42 小初 36
一号 26 小一 24
二号 22 小二 18
三号 16 小三 15
四号 14 小四 12
五号 10.5 小五 9
六号 7.5 小六 6.5
七号 5.5 八号 5

Requirements

  • Python >= 3.10
  • markdown2
  • python-docx
  • html-for-docx
  • httpx
  • Pillow
  • latex2mathml
  • mathml2omml
  • lxml

License

MIT

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

md2word-0.1.1.tar.gz (73.0 kB view details)

Uploaded Source

Built Distribution

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

md2word-0.1.1-py3-none-any.whl (21.4 kB view details)

Uploaded Python 3

File details

Details for the file md2word-0.1.1.tar.gz.

File metadata

  • Download URL: md2word-0.1.1.tar.gz
  • Upload date:
  • Size: 73.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for md2word-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5cedee799e64856532c046c7d64df0dfa80169164b1fffbb9cf7ee346d746e5d
MD5 b2fe425edeac230679e24aaa8a858347
BLAKE2b-256 fde45301f11268d580be61dd1f676dbf853197066598504c201bd00eb7aeb576

See more details on using hashes here.

Provenance

The following attestation bundles were made for md2word-0.1.1.tar.gz:

Publisher: publish.yml on AndersonBY/md2word

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

File details

Details for the file md2word-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: md2word-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 21.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for md2word-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 197a3d9ee8f70ed2ea569c2900d7424f4ff8b4d75198f44a41dbc6efa28c2bbd
MD5 2ff2e9954cc9a3ddc6373c1e984f553a
BLAKE2b-256 1ccd330a8cf8acdc7c9859b32662dc46e2951078a2a50406ec95d4f3ca2af6e2

See more details on using hashes here.

Provenance

The following attestation bundles were made for md2word-0.1.1-py3-none-any.whl:

Publisher: publish.yml on AndersonBY/md2word

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

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