@clipg/w2w
v1.1.1
Published
Markdown to Word (DOCX) converter with LaTeX math support
Maintainers
Readme
w2w
将 Markdown 文档转换为 Word(DOCX)文件,支持 LaTeX 数学公式渲染为 Word 原生公式。
项目地址
https://github.com/CliPg/md2word
功能特性
- LaTeX 数学公式 — 将
$...$和$$...$$渲染为 Word 原生公式(OMML),无法转换时降级为 Unicode 文本 - 完整 Markdown 支持 — 标题、段落、加粗、斜体、删除线、行内代码、代码块、引用、有序/无序列表、表格、分割线
- 图片支持 — 支持本地绝对路径、相对路径和网络图片(HTTP/HTTPS)
- 自定义排版 — 可配置字体、字号、行高和标题样式
- 命令行与 API 双模式 — 既可作为命令行工具使用,也可作为 Node.js 库调用
安装
npm install @clipg/w2w命令行使用
# 全局安装
npm install -g @clipg/w2w
# 转换文件
w2w document.md
w2w input.md output.docx默认输出与输入文件同名、扩展名为 .docx 的文件。
自定义排版
# 正文样式
w2w demo.md --body-font "宋体" --body-size 10.5 --body-color "#333333"
# 标题样式(支持 h1-h4)
w2w demo.md --h1-font "黑体" --h1-size 16 --h1-color "#000000" --h1-bold
w2w demo.md --h2-font "黑体" --h2-color "#00008B" --h2-align left
# 全局行间距
w2w demo.md --line-height 1.5
# 组合使用
w2w demo.md output.docx \
--body-font "宋体" --body-size 10.5 --body-color "#000000" \
--h1-font "黑体" --h1-size 16 \
--h2-font "黑体" --h2-color "#00008B" \
--line-height 1.5可用参数:
| 参数 | 说明 |
|------|------|
| --body-font | 正文字体 |
| --body-size | 正文字号(磅值) |
| --body-color | 正文颜色(如 #333333) |
| --h1-font / --h2-font / --h3-font / --h4-font | 各级标题字体 |
| --h1-size / --h2-size / --h3-size / --h4-size | 各级标题字号 |
| --h1-color / --h2-color / --h3-color / --h4-color | 各级标题颜色 |
| --h1-bold / --h2-bold / --h3-bold / --h4-bold | 标题加粗 |
| --h1-center / --h2-center / --h3-center / --h4-center | 标题居中 |
| --h1-align / --h2-align / --h3-align / --h4-align | 标题对齐(left/center/right/justify) |
| --h1-spacing-before / --h2-spacing-before / ... | 段前间距 |
| --h1-spacing-after / --h2-spacing-after / ... | 段后间距 |
| --line-height | 全文行间距倍数 |
Skill
安装
npx skills add CliPg/md2word安装后在 Claude Code 中直接描述排版需求即可,例如:
把 demo.md 转成 Word,格式要求:
- 标题:三号黑体
- 题目:五号黑体,深蓝色
- 正文:五号宋体,黑色
- 全文:1.5 倍行间距,段前 0.5 行Skill 会自动将中文字号、字体、颜色映射为 FormatSettings 并执行转换。
效果展示
md文件:
转换后的word文件:

编程接口
基本用法
import { convertMdToDocx } from '@clipg/w2w';
import * as fs from 'fs';
const md = fs.readFileSync('document.md', 'utf-8');
const buffer = await convertMdToDocx(md, {
sourceFilePath: '/path/to/document.md', // 用于解析相对路径图片
});
fs.writeFileSync('document.docx', buffer);自定义排版
import { convertMdToDocx } from '@clipg/w2w';
const buffer = await convertMdToDocx(md, {
formatSettings: {
paragraph: {
fontFamily: '宋体',
fontSize: 12,
lineHeight: 1.5,
paragraphSpacing: 6,
firstLineIndent: 2,
},
heading1: {
fontFamily: '黑体',
fontSize: 22,
lineHeight: 1.5,
alignment: 'center',
spacingBefore: 12,
spacingAfter: 12,
},
heading2: {
fontFamily: '黑体',
fontSize: 16,
lineHeight: 1.5,
alignment: 'left',
spacingBefore: 12,
spacingAfter: 6,
},
heading3: {
fontFamily: '黑体',
fontSize: 14,
lineHeight: 1.5,
alignment: 'left',
spacingBefore: 6,
spacingAfter: 6,
},
heading4: {
fontFamily: '黑体',
fontSize: 12,
lineHeight: 1.5,
alignment: 'left',
spacingBefore: 6,
spacingAfter: 6,
},
},
});自定义图片加载
const buffer = await convertMdToDocx(md, {
imageLoader: async (imagePath) => {
// 返回图片 Buffer 或 null
return null;
},
});LaTeX 公式支持
行内公式和块级公式通过 latex-to-omml 转换为 Word 原生公式对象。转换失败时会降级为 Unicode 文本,仍可显示上标、下标和希腊字母。
行内公式:$E = mc^2$
块级公式:
$$
\int_0^\infty e^{-x^2} dx = \frac{\sqrt{\pi}}{2}
$$API 参数
ConvertOptions
| 参数 | 类型 | 说明 |
|------|------|------|
| formatSettings | FormatSettings | 字体、字号、行高和间距配置 |
| sourceFilePath | string | 源文件路径,用于解析相对路径图片 |
| imageLoader | (path: string) => Promise<Buffer \| null> | 自定义图片加载函数 |
开发
npm install
npm run build许可证
MIT
