支持读取Word文档(.docx/.doc)及中文,可提取文本、搜索关键词并保存为UTF-8文本文件,无需安装Word。
读取 Word Document 6是一项面向实际任务的技能,主要用于一个读取 Microsoft Word 文档的专业工具,支持现代的.docx 和 遗留的.doc 格式,并配有完整的中文语言支持.;Features.;
从功能定位来看,该技能强调把分散的操作要求整理成清晰、可复用的处理流程,使用户能够围绕既定目标快速准备输入、选择执行方式并获得结构化结果。实际使用前应先确认任务范围、数据来源、运行环境、必要权限和关键参数,再依据技能说明逐步执行;若输入条件不完整,应先补齐信息或采用保守配置,避免因错误假设导致结果偏离需求。
执行过程中需要关注工具调用是否成功、接口或依赖是否可用、输出格式是否符合预期,并对异常提示、缺失字段和边界情况进行处理;涉及批量任务时,还应保存进度,避免中断后重复操作。该技能适合用于一次性任务,也可以接入自动化工作流,与其他技能或上层代理配合完成更完整的业务链路;在组合使用时,应明确每一步的输入输出关系,并避免不同步骤之间出现参数冲突。
一款专业的 Microsoft Word 文档读取工具,同时支持现代 .docx 格式与传统的 .doc 格式,并完整支持中文语言环境。
pip install python-docx olefile
# 复制到你的 OpenClaw skills 目录
cp -r read-word ~/.openclaw/skills/
# 基础读取(显示前 100 个段落)
python ~/.openclaw/skills/read-word/read_word.py "document.docx"
# 显示更多内容
python ~/.openclaw/skills/read-word/read_word.py "document.docx" --limit 200
# 关键词搜索
python ~/.openclaw/skills/read-word/read_word.py "document.docx" --search "keyword1,keyword2"
# 保存为文本文件
python ~/.openclaw/skills/read-word/read_word.py "document.docx" --output "output.txt"
# 仅显示文档信息
python ~/.openclaw/skills/read-word/read_word.py "document.docx" --info
# 方法 1:导入函数
import sys
sys.path.insert(0, '~/.openclaw/skills/read-word')
from read_word import read_word_document, search_in_document
# 读取文档
paragraphs = read_word_document("document.docx")
for para in paragraphs:
print(para)
# 搜索关键词
results = search_in_document("document.docx", ["keyword1", "keyword2"])
from read_word import read_word_document
paragraphs = read_word_document("report.docx")
print(f"Document has {len(paragraphs)} paragraphs")
# 显示前 10 个段落
for i, p in enumerate(paragraphs[:10]):
print(f"{i+1}. {p}")
from read_word import search_in_document
# 查找包含 "kitchen" 或 "feng shui" 的段落
results = search_in_document("book.docx", ["kitchen", "feng shui"])
for r in results:
print(r)
from pathlib import Path
from read_word import read_word_document
desktop = Path.home() / "Desktop"
for doc_file in desktop.glob("*.docx"):
paragraphs = read_word_document(doc_file)
print(f"{doc_file.name}: {len(paragraphs)} paragraphs")
read_word_document(filepath)读取 Word 文档并返回段落字符串列表。
参数:
filepath(str|Path):Word 文档路径返回值:
list:段落字符串列表异常:
FileNotFoundError:文件不存在时抛出ValueError:文件格式不受支持时抛出search_in_document(filepath, keywords)在 Word 文档中搜索关键词。
参数:
filepath(str|Path):Word 文档路径keywords(list):待搜索的关键词列表返回值:
list:匹配的段落列表,格式为 "[Paragraph N] content"save_as_text(paragraphs, output_path)将段落列表保存为 UTF-8 编码的文本文件。
参数:
paragraphs(list):段落字符串列表output_path(str|Path):输出文件路径analyze_document(filepath)分析文档并返回统计信息。
返回值:
dict:包含文件名、文件大小、段落数量、总字符数等字段解决方案: pip install python-docx
原因: OLE 解析对复杂格式支持有限
解决方案: 使用 Microsoft Word 将 .doc 文件转换为 .docx 后再读取
原因: 终端编码不兼容
解决方案: 使用 --output 参数保存至文件,再用编辑器打开
| 格式 | 扩展名 | 支持等级 |
|---|---|---|
| Word 2007+ | .docx | 完全支持 |
| Word 97–2003 | .doc | 部分支持(仅文本) |
| Word 95 / 6.0 | .doc | 不支持 |
| 富文本格式 | .rtf | 不支持 |
--output 时生成 .txt 输出文件风险等级:低 —— 仅执行本地文件操作,不访问网络,原始文件不会被修改。
叶文洁(Ye Wenjie)—— 专为阅读风水典籍及 Word 文档而开发
MIT License