Skip to content
Back to skills

176 Md2docx

ASecurity

Markdown 与 Word 双向转换,支持合并单元格(rowspan/colspan)、Mermaid 图表渲染、代码高亮、中文排版优化

  • 4 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added November 29, 2026
developmentpythonbashnode

Works with

  • cli

Security analysis

A92/100
  • mediumInstalls packages at runtime which could introduce malicious dependencies
  • mediumInstalls packages at runtime which could introduce malicious dependencies

Pro scans all 7 files and shows the line behind each finding

Scanned November 29, 2026

npx -y skills add winlem2024/workbuddy-skills --skill 176-md2docx --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of 176 Md2docx?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for 176 Md2docx
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/winlem2024-176-md2docx/badge)](https://www.skillsdirectory.com/skills/winlem2024-176-md2docx)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: "md2docx"
description: "Markdown 与 Word 双向转换,支持合并单元格(rowspan/colspan)、Mermaid 图表渲染、代码高亮、中文排版优化"
---

# md2docx — Markdown ↔ Word 双向转换

支持 `.md` 和 `.docx` 之间的双向转换,自动识别文件类型。

## 用法

```bash
# Markdown → Word
python md2docx.py input.md output.docx

# Word → Markdown
python md2docx.py input.docx output.md
```

脚本根据文件扩展名自动判断转换方向。

## 功能

### MD → DOCX
- **中文排版**:全文微软雅黑,正文 10.5pt,表格 9pt
- **动态标题字号**:自动检测标题层级,末级 12.5pt,每升一级 +2pt
- **Mermaid 图表**:自动渲染为 PNG 嵌入文档,动态检测系统 Chrome/Edge
- **代码块**:灰色背景 + 四边框 + Consolas 等宽字体
- **行内格式**:`**粗体**`、`*斜体*`、`` `代码` ``、`[链接](url)`
- **表格**:表头灰底,支持 HTML 转义
- **HTML 表格**:支持合并单元格(rowspan/colspan),虚拟网格算法还原
- **引用块 / 分割线 / 图片 / HTML 块**

### DOCX → MD
- **完整结构提取**:标题、段落、列表、表格全部保留
- **合并单元格**:HTML `<table>` 直接嵌入 MD(绕过 MD 不支持合并的限制)
- **图片提取**:内联图片转为 MD 图片引用
- **Mermaid 图表**:代码块原样保留

### 合并单元格方案

Markdown 表格语法不支持 `rowspan` / `colspan`,本工具采用 **HTML 嵌入方案**:

```
DOCX → MD:mammoth 转 HTML → HTML <table> 嵌入 .md
MD → DOCX:解析 HTML <table> → 虚拟网格算法 → cell.merge() 还原
```

MD 文件中嵌入的 HTML 表格能被主流 Markdown 渲染器正确显示,反向还原时也能精确恢复合并结构。

## 依赖

### Python 3.8+

```bash
pip install python-docx pygments pillow mammoth beautifulsoup4
```

| 包 | 必须 | 用途 |
|---|---|---|
| `python-docx` | 是 | 生成/解析 Word 文档 |
| `mammoth` | 是 | DOCX → HTML 转换(保留合并单元格) |
| `beautifulsoup4` | 是 | HTML 表格解析 |
| `pygments` | 否 | 代码块语法高亮(缺失时回退纯文本) |
| `pillow` | 否 | Mermaid 图片自动缩放(缺失时用默认宽度) |

### Node.js(Mermaid 渲染,可选)

如需 MD→DOCX 时渲染 Mermaid 图表:

```bash
cd <skill目录>
npm install
```

安装后 `node_modules/.bin/mmdc.cmd` 会被自动调用。

**浏览器配置**:脚本会自动检测系统已安装的 Chrome / Edge / Chromium,无需手动下载。若系统无浏览器,Mermaid 以文字占位符代替。

若不需要 Mermaid 支持,可跳过 `npm install`。

## 个性化配置

在 `md2docx.py` 顶部修改常量:

```python
FONT_NAME         = '微软雅黑'  # 全文字体
BODY_FONT_SIZE    = 10.5        # 正文字号
TABLE_FONT_SIZE   = 9           # 表格字号
MIN_HEADING_SIZE  = 12.5        # 最末级标题字号
HEADING_SIZE_STEP = 2           # 每升一级递增字号
```

## 文件结构

```
md2docx/
├── md2docx.py        # 主转换脚本(双向)
├── code_block.py     # 代码块样式渲染
├── SKILL.md          # Skill 描述(本文件)
├── README.md         # 详细文档
└── package.json      # Node.js 依赖(Mermaid CLI)
```

Files in this skill

  • README.md5.4 KB
  • SKILL.md3.2 KB
  • _meta.json100 B
  • _skillhub_meta.json535 B
  • code_block.py3 KB
  • md2docx.py51 KB
  • package.json300 B

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.