name: python-docx-creator description: "使用 python-docx 庫生成 Word 文件(.docx)。當用戶需要將 Markdown 或純文本內容轉換為格式化的 Word 文件時使用此技能。觸發條件包括:要求生成 Word 文件、將內容儲存到 .docx 檔案、建立專業格式的文件。"
使用 python-docx 庫將 Markdown 或純文本內容轉換為格式化的 Word 文件。
使用 scripts/create_docx.py 生成 Word 文件:
# 從文本建立
python3 scripts/create_docx.py --title "標題" --text "# 內容\n\n段落文本" --output output.docx
# 從檔案建立
python3 scripts/create_docx.py --title "標題" --input input.md --output output.docx
# H1、## H2、### H3- 專案 或 * 專案1. 專案、2. 專案**粗體文本** 或 __粗體文本__*斜體文本* 或 _斜體文本_| 引數 | 說明 | 必填 |
|---|---|---|
--title |
文件標題 | 否 |
--input |
輸入的 Markdown 檔案路徑 | 否* |
--text |
直接輸入的文本內容 | 否* |
--output |
輸出的 Word 文件路徑 | 是 |
*注:--input 和 --text 至少提供一個
python3 -c "import docx; print('python-docx installed')" 2>&1 || echo "NOT_INSTALLED"
如果未安裝,執行:
pip install python-docx
選項 A:從檔案讀取
# 文件標題
## 章節一
這是第一段內容。
- 列表項1
- 列表項2
**重點內容** 和 *強調內容*
選項 B:直接使用文本 在命令列中傳遞 Markdown 格式的文本內容。
# 示例:生成知識庫說明文件
python3 scripts/create_docx.py \
--title "什麼是知識庫?" \
--text "# 什麼是知識庫?\n\n知識庫是結構化儲存、組織和檢索知識的系統...\n\n## 從產品視角看\n\n1. 企業知識庫\n2. AI 知識庫" \
--output /Users/apple/Desktop/輸出文件.docx
ls -lh <output-file>.docx
當用戶要求"把輸出結果轉換為 Word 文件並儲存"時:
create_docx.py 生成 .docx 檔案# 迴圈生成多個文件
for file in *.md; do
python3 scripts/create_docx.py \
--input "$file" \
--output "${file%.md}.docx"
done
如需支援更多 Word 格式(表格、圖片、樣式等),可以擴充套件 create_docx.py:
doc.add_table()doc.add_picture()doc.styles7w4.net提供免費和付費技能下載。
這個 Skill 質量中規中矩,文件寫得清楚明瞭,新手也能快速上手使用。優點是功能專注、介面簡潔,能滿足基本的文件轉換需求。缺點是功能比較單一,不支援表格、圖片這類常用格式,也沒有測試保障穩定性。如果只是偶爾需要轉幾份簡單的文件,可以用;但如果對文件格式要求較高,可能不太夠用。