docx-shell-extract
它能做什么
这项技能展示如何通过解压 .docx 文件并用标准命令行工具去除标记来读取它。它适合在不安装额外软件的情况下获取 Word 文字的人。
适用场景
- ✓当我电脑上没有 python-docx 或类似库的时候。
- ✓当我需要在不安装任何东西的情况下,从 Word 文件中快速提取文字的时候。
- ✓当环境受限,只能使用 shell 工具的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/docx-shell-extract/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“我需要这个 Word 文件中的文字,但无法安装任何额外软件。你能用命令行把它提取出来吗?”
每周 5 个新技能,1 分钟读完。
工作原理
一个 .docx 文件是内含 XML 的压缩包。AI 会提取主文档 XML,去除标签和空行,并将结果保存为纯文本。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
DOCX Shell Extraction
When to Use This Skill
Use this pattern when you need to read or extract text from Microsoft Word (.docx) files in constrained environments where:
- The
python-docxlibrary is not available - You cannot install additional Python packages
- You need a quick, reliable shell-based solution
Core Technique
DOCX files are ZIP archives containing XML files. The main document content is stored in word/document.xml. You can extract and parse this using standard shell tools.
Step-by-Step Instructions
Step 1: Extract the document.xml content
unzip -p filename.docx word/document.xml
The -p flag pipes the content to stdout without extracting to disk.
Step 2: Strip XML tags to get plain text
unzip -p filename.docx word/document.xml | sed 's/<[^>]*>//g'
This removes all XML tags, leaving the text content.
Step 3: Clean up whitespace (optional)
For cleaner output, add additional sed processing:
unzip -p filename.docx word/document.xml | \
sed 's/<[^>]*>//g' | \
sed 's/&[^;]*;//g' | \
sed 's/^[[:space:]]*//' | \
sed 's/[[:space:]]*$//' | \
sed '/^$/d'
This removes:
- XML tags
- XML entities (like
&,<) - Leading/trailing whitespace
- Empty lines
Step 4: Save to a text file (optional)
unzip -p filename.docx word/document.xml | \
sed 's/<[^>]*>//g' > output.txt
Complete Example
# Extract text from a Word document
DOCX_FILE="report.docx"
OUTPUT_FILE="report_text.txt"
unzip -p "$DOCX_FILE" word/document.xml | \
sed 's/<[^>]*>//g' | \
sed 's/&[^;]*;//g' | \
sed '/^$/d' > "$OUTPUT_FILE"
echo "Extracted text saved to $OUTPUT_FILE"
Verification
After extraction, verify the content was captured:
# Check if output file has content
if [ -s "$OUTPUT_FILE" ]; then
echo "Successfully extracted $(wc -l < "$OUTPUT_FILE") lines"
head -5 "$OUTPUT_FILE"
else
echo "Warning: Output file is empty"
fi
Limitations
- This method extracts raw text without formatting
- Complex layouts, tables, and images are not preserved
- Some special characters may need additional handling
- Works best for text-heavy documents
Alternatives to Explore
If this approach fails or the DOCX structure differs:
- Check for
word/document.xmlexistence:unzip -l filename.docx | grep document.xml - Some documents may use
word/*.xmlwith different naming - Consider
pandocif available:pandoc filename.docx -t plain
常见问题
为什么 Word 文件像压缩包?
因为 .docx 文件是一个压缩包,里面包含存放文档内容的 XML 文件。
格式和表格会保留吗?
这种方法会去除标记,因此只返回纯文本,而不是原来的版式。
结果可以保存吗?
可以。指南展示了如何把提取的文字写入文件。
详情
作者的原始描述
Extract text from DOCX files using shell commands when python-docx is unavailable
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12867
- 许可证
- MIT
- 更新时间
- 2026/11/1
评论
- …
同一仓库的更多技能
agentpilot
7.8k帮你的智能体准备任务并运行 AgentPilot 循环,逐步改进项目:循环会提出修改、测试修改并为其打分。
openspace
7.8k帮你的智能体设置 OpenSpace,这是一个用于存储和获取智能体技能的工具,并通过 MCP 将其连接到你的 AI 应用。
rsi-arena
7.8k帮你的智能体设置并运行标准 AI 基准测试,例如 AIME 和 Terminal-Bench,用来比较不同的改进方法。
experiment-guide
7.8k帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。