Install
openclaw skills install markdown-to-word-skillConvert Markdown files to well-formatted Word documents with support for headings, lists, tables, code blocks, images, math formulas, custom styles, template...
openclaw skills install markdown-to-word-skill使用Python的python-docx、markdown、beautifulsoup4库将Markdown文件转换为格式良好的Word文档。支持标题、段落、列表、表格、代码块、图片等Markdown元素的转换。
当用户需要将Markdown文件转换为Word文档时激活此技能。
markdown-to-word-skill/
├── SKILL.md # 技能定义文件
├── README.md # 详细文档
├── requirements.txt # Python依赖
├── install.sh # 安装脚本
├── quick_start.sh # 快速开始脚本
├── scripts/
│ ├── md2docx.py # 主转换脚本
│ ├── md2docx_batch.py # 批量转换脚本
│ ├── md2docx_with_images.py # 带图片转换脚本
│ └── template_processor.py # 模板处理器
├── templates/
│ ├── academic.docx # 学术论文模板
│ ├── business.docx # 商业报告模板
│ └── technical.docx # 技术文档模板
├── examples/
│ ├── sample.md # 示例Markdown文件
│ ├── sample_output.docx # 示例输出
│ └── test_images/ # 测试图片目录
└── config/
└── styles.json # 样式配置
pip install python-docx markdown beautifulsoup4 pillow
# Ubuntu/Debian
sudo apt-get install python3-pip python3-venv
# macOS
brew install python3
python scripts/md2docx.py --input sample.md --output sample.docx
python scripts/md2docx.py --input sample.md --output sample.docx --template templates/academic.docx
python scripts/md2docx_batch.py --input-dir ./markdown_files --output-dir ./word_docs
python scripts/md2docx_with_images.py --input sample.md --output sample.docx --image-dir ./images
"""
Markdown转Word主转换脚本
支持:
- 标题(H1-H6)
- 段落和文本格式(粗体、斜体、删除线)
- 列表(有序、无序)
- 表格
- 代码块
- 引用块
- 水平线
- 图片
- 链接
"""
# 基本用法
python md2docx.py --input input.md --output output.docx
# 高级选项
python md2docx.py --input input.md --output output.docx \
--template academic.docx \
--style-title "Title" \
--style-heading1 "Heading 1" \
--style-heading2 "Heading 2" \
--style-paragraph "Normal" \
--style-code "Code" \
--style-quote "Quote"
"""
批量转换脚本
支持:
- 批量处理文件夹中的所有Markdown文件
- 递归处理子文件夹
- 保持目录结构
- 生成转换报告
"""
# 基本用法
python md2docx_batch.py --input-dir ./docs --output-dir ./word_docs
# 高级选项
python md2docx_batch.py --input-dir ./docs --output-dir ./word_docs \
--recursive \
--template technical.docx \
--report report.json \
--skip-existing
"""
带图片处理的转换脚本
支持:
- 自动查找并插入图片
- 图片大小调整
- 图片居中/对齐
- 图片标题
- 相对路径支持
"""
# 基本用法
python md2docx_with_images.py --input article.md --output article.docx --image-dir ./images
# 高级选项
python md2docx_with_images.py --input article.md --output article.docx \
--image-dir ./images \
--image-width 500 \
--image-height 300 \
--image-quality 85 \
--add-captions
# 一级标题
## 二级标题
### 三级标题
**粗体文本**
*斜体文本*
~~删除线文本~~
- 无序列表项1
- 无序列表项2
1. 有序列表项1
2. 有序列表项2
| 列1 | 列2 | 列3 |
|-----|-----|-----|
| 数据1 | 数据2 | 数据3 |
| 数据4 | 数据5 | 数据6 |
```python
def hello_world():
print("Hello, World!")
```

[链接文本](https://example.com)
> 这是一段引用文字
> 可以有多行
---
{
"h1": "Heading 1",
"h2": "Heading 2",
"h3": "Heading 3",
"h4": "Heading 4",
"h5": "Heading 5",
"h6": "Heading 6",
"p": "Normal",
"code": "Code",
"quote": "Quote",
"table": "Table Grid",
"image": "Image Caption"
}
创建 config/styles.json:
{
"styles": {
"title": "自定义标题",
"heading1": "自定义标题1",
"heading2": "自定义标题2",
"paragraph": "自定义段落",
"code": "代码样式",
"quote": "引用样式"
},
"font": {
"title": {"name": "微软雅黑", "size": 24, "bold": true},
"heading1": {"name": "微软雅黑", "size": 18, "bold": true},
"paragraph": {"name": "宋体", "size": 12}
},
"colors": {
"title": "2E74B5",
"heading1": "2E74B5",
"code_background": "F2F2F2"
}
}
academic.docx - 学术论文模板
business.docx - 商业报告模板
technical.docx - 技术文档模板
.docx 文件到 templates/ 目录--template 参数指定#!/bin/bash
# Markdown转Word技能安装脚本
echo "安装Markdown转Word技能..."
# 创建虚拟环境
python3 -m venv venv
source venv/bin/activate
# 安装Python依赖
pip install python-docx markdown beautifulsoup4 pillow
# 创建必要目录
mkdir -p templates examples test_images config
echo "✅ 安装完成!"
echo "激活虚拟环境:source venv/bin/activate"
echo "运行测试:python scripts/md2docx.py --input examples/sample.md --output examples/sample_output.docx"
#!/bin/bash
# 快速开始脚本
echo "Markdown转Word技能快速开始..."
# 检查虚拟环境
if [ ! -d "venv" ]; then
echo "创建虚拟环境..."
python3 -m venv venv
fi
# 激活虚拟环境
source venv/bin/activate
# 安装依赖
pip install -q python-docx markdown beautifulsoup4 pillow
# 运行示例
echo "运行示例转换..."
python scripts/md2docx.py --input examples/sample.md --output examples/sample_output.docx
if [ $? -eq 0 ]; then
echo "✅ 示例转换成功!"
echo "输出文件:examples/sample_output.docx"
else
echo "❌ 转换失败,请检查错误信息"
fi
# 示例文档
这是一个Markdown转Word的示例文档。
## 章节1:介绍
这是第一章节的内容。**粗体文本**和*斜体文本*都可以正常转换。
### 子章节1.1
这是一个子章节。
## 章节2:列表示例
### 无序列表
- 项目1
- 项目2
- 子项目2.1
- 子项目2.2
- 项目3
### 有序列表
1. 第一步
2. 第二步
3. 第三步
## 章节3:表格示例
| 姓名 | 年龄 | 职业 |
|------|------|------|
| 张三 | 25 | 工程师 |
| 李四 | 30 | 设计师 |
| 王五 | 28 | 产品经理 |
## 章节4:代码示例
```python
def calculate_sum(a, b):
"""计算两个数的和"""
return a + b
result = calculate_sum(10, 20)
print(f"结果: {result}")
这是引用内容 可以有多行
— 引用来源

文档结束。
## 故障排除
### 常见问题
#### 1. 缺少依赖
ModuleNotFoundError: No module named 'docx'
**解决方案**:
```bash
pip install python-docx
FileNotFoundError: [Errno 2] No such file or directory: 'image.jpg'
解决方案:
--image-dir 参数指定图片目录KeyError: 'Heading 1'
解决方案:
--list-styles 查看可用样式UnicodeDecodeError: 'utf-8' codec can't decode byte...
解决方案:
python md2docx.py --input input.md --output output.docx --encoding utf-8-sig
python md2docx.py --input input.md --output output.docx --debug
# 转换单个文件
exec("cd /path/to/markdown-to-word-skill && python scripts/md2docx.py --input document.md --output document.docx")
# 批量转换
exec("cd /path/to/markdown-to-word-skill && python scripts/md2docx_batch.py --input-dir ./docs --output-dir ./word_docs")
def convert_markdown_to_word(md_file, docx_file, template=None):
"""转换Markdown到Word"""
import subprocess
cmd = ["python", "scripts/md2docx.py", "--input", md_file, "--output", docx_file]
if template:
cmd.extend(["--template", template])
result = subprocess.run(cmd, capture_output=True, text=True)
if result.returncode == 0:
return {"success": True, "output": docx_file}
else:
return {"success": False, "error": result.stderr}
from md2docx import MarkdownToDocx
class CustomConverter(MarkdownToDocx):
def convert_custom_element(self, element):
"""处理自定义Markdown元素"""
pass
# 创建插件
class ImagePlugin:
def process(self, converter, element):
"""处理图片元素"""
pass
# 注册插件
converter.register_plugin(ImagePlugin())
from flask import Flask, request, send_file
app = Flask(__name__)
@app.route('/convert', methods=['POST'])
def convert():
md_content = request.form['markdown']
# 转换逻辑
return send_file('output.docx')
MIT License
技能创建时间:2026-03-02 10:12 GMT+8 创建者:OpenClaw AI Assistant 版本:1.0.0