md-to-docx-template

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Markdown to DOCX with Templates

基于模板的Markdown转DOCX

Convert markdown to professionally styled Word documents. Not plain exports — beautiful, branded documents with your fonts, colors, and table styling applied automatically.
将Markdown转换为具有专业样式的Word文档。并非普通导出——而是会自动应用您的字体、颜色和表格样式的精美品牌化文档。

When to Use

适用场景

  • User wants to convert markdown to Word/DOCX
  • User needs a branded document from markdown source
  • User asks about exporting markdown with styling
  • User mentions "md to docx", "markdown to word", or similar
  • 用户需要将Markdown转换为Word/DOCX格式
  • 用户希望从Markdown源文件生成品牌化文档
  • 用户询问如何导出带有样式的Markdown
  • 用户提及“md转docx”、“markdown转word”或类似需求

Prerequisites

前置条件

  • pandoc
    installed (
    brew install pandoc
    on macOS)
  • Python 3.8+ with packages:
    python-docx
    ,
    lxml
    ,
    click
    ,
    pyyaml
  • 已安装
    pandoc
    (macOS系统可通过
    brew install pandoc
    安装)
  • Python 3.8及以上版本,并安装以下包:
    python-docx
    ,
    lxml
    ,
    click
    ,
    pyyaml

Installation

安装步骤

Clone this skill's scripts to your preferred location:
bash
git clone https://github.com/jonnyschneider/skills.git
cd skills/skills/md-to-docx-template
pip install python-docx lxml click pyyaml
将本技能的脚本克隆到您偏好的位置:
bash
git clone https://github.com/jonnyschneider/skills.git
cd skills/skills/md-to-docx-template
pip install python-docx lxml click pyyaml

Quick Conversion

快速转换

bash
./md-to-docx.py input.md -o output.docx
bash
./md-to-docx.py input.md -o output.docx

With Custom Template

使用自定义模板

Option A: Generate template with specific fonts/colors

选项A:生成带有指定字体/颜色的模板

bash
./create-reference-template.py template.docx \
  --font-body "DM Sans" \
  --font-heading "DM Sans" \
  --accent-color "0D494D" \
  --heading-color "0D494D"

./md-to-docx.py input.md -o output.docx --template template.docx
bash
./create-reference-template.py template.docx \
  --font-body "DM Sans" \
  --font-heading "DM Sans" \
  --accent-color "0D494D" \
  --heading-color "0D494D"

./md-to-docx.py input.md -o output.docx --template template.docx

Option B: Extract styles from existing branded document

选项B:从现有品牌化文档中提取样式

bash
undefined
bash
undefined

Full pipeline for best results:

最佳效果的完整流程:

./extract-styles.py source-branded.docx template.docx ./md-to-docx.py input.md -o output.docx --template template.docx --no-fix-indent ./apply-template-styles.py template.docx output.docx ./fix-table-headers.py output.docx
undefined
./extract-styles.py source-branded.docx template.docx ./md-to-docx.py input.md -o output.docx --template template.docx --no-fix-indent ./apply-template-styles.py template.docx output.docx ./fix-table-headers.py output.docx
undefined

Multi-file Documents

多文件文档转换

Create a
manifest.yaml
:
yaml
template: template.docx
output: final-document.docx
sections:
  - 01-intro.md
  - 02-content.md
  - 03-conclusion.md
Then run:
bash
./md-to-docx.py manifest.yaml --open
创建
manifest.yaml
文件:
yaml
template: template.docx
output: final-document.docx
sections:
  - 01-intro.md
  - 02-content.md
  - 03-conclusion.md
然后运行:
bash
./md-to-docx.py manifest.yaml --open

Key Options

关键选项

OptionDescription
-o, --output
Output DOCX path
--template
Custom reference template
--toc
Generate table of contents
--open
Open after building
--no-fix-indent
Skip list indent fix (use with extract pipeline)
选项描述
-o, --output
输出DOCX文件路径
--template
自定义参考模板
--toc
生成目录
--open
转换完成后自动打开文档
--no-fix-indent
跳过列表缩进修复(配合样式提取流程使用)

Template Options

模板选项

OptionDescription
--font-body
Body text font (default: Calibri)
--font-heading
Heading font (default: Calibri)
--font-mono
Code font (default: Consolas)
--accent-color
Table header color (hex, default: 4472C4)
--heading-color
Heading text color (hex, default: 000000)
选项描述
--font-body
正文字体(默认:Calibri)
--font-heading
标题字体(默认:Calibri)
--font-mono
代码字体(默认:Consolas)
--accent-color
表格标题颜色(十六进制值,默认:4472C4)
--heading-color
标题文字颜色(十六进制值,默认:000000)

Limitations

限制说明

  • Custom fonts must be installed on target machines
  • Complex tables may not render perfectly
  • SVG images require
    rsvg-convert
  • Some pandoc quirks require post-processing scripts
  • 自定义字体必须已安装在目标机器上
  • 复杂表格可能无法完美渲染
  • SVG图片需要依赖
    rsvg-convert
    工具
  • Pandoc的一些特性需要通过后处理脚本修复