使用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"7w4.net有更好的技能外掛。
"""
批次轉換指令碼
支援:
- 批次處理資料夾中的所有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"
}
}
支援章節編號
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
這個 Skill 質量較差,核心程式碼檔案不完整,主要指令碼存在缺失內容無法正常執行。文件承諾的功能與實際提供的檔案不符,很多描述的功能根本找不到。雖然功能設計看起來比較完善(有批次轉換、圖片處理等),但由於程式碼不完整,實際可用性很低,不建議直接使用。建議等待開發者修復後再嘗試。