docx-parse-resilient
它能做什么
这项技能是读取 .docx 文件的两步方法,当 shell 方法失败时由 Python 备用方案接手。它适合在容器或工具较少、环境各异的简化设置中工作的人。
适用场景
- ✓当 unzip 命令在 Word 文件上返回错误或没有输出的时候。
- ✓当我在容器、CI 任务或工具很少的简化环境中工作的时候。
- ✓当我需要可靠地提取 Word 文字,并能自动切换到备用方法的时候。
技能链接
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/docx-shell-parse-enhanced/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请提取这个 Word 文件中的文字。如果 unzip 命令没有输出,就改用 Python 再试一次。”
每周 5 个新技能,1 分钟读完。
工作原理
AI 先运行 shell 提取,检查是否返回了任何文字;如果结果为空,就用 Python 的 zipfile 模块读取 Word 文件的 XML。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
Resilient DOCX Text Extraction
Extract text from Microsoft Word (.docx) files using a robust two-tier approach: shell-based extraction as the primary method, with Python zipfile fallback when shell commands fail or return no output.
When to Use
- Python environment may lack
python-docxbutzipfilemodule is available (standard library) - Working in constrained or inconsistent environments (containers, minimal images, CI/CD)
- Shell
unzipcommand returns errors or no output - Need reliable extraction with automatic fallback
Core Technique
DOCX files are ZIP archives containing XML files. This skill provides two extraction methods:
- Primary (Shell):
unzip -p+sedfor fast extraction - Fallback (Python):
zipfilemodule for reliable extraction when shell fails
Step-by-Step Instructions
1. Verify the DOCX file exists
ls -la document.docx
2. Test shell extraction first (recommended)
Try the shell-based approach:
unzip -p document.docx word/document.xml 2>/dev/null | sed -e 's/<[^>]*>//g'
3. Check if shell extraction produced output
Verify the shell method returned content:
content=$(unzip -p document.docx word/document.xml 2>/dev/null | sed -e 's/<[^>]*>//g')
if [ -z "$content" ]; then
echo "Shell extraction returned no output, trying Python fallback..."
fi
4. Use Python zipfile fallback if needed
When shell commands fail or return empty output, use Python's standard zipfile module:
python3 -c "
import zipfile
import sys
import re
try:
with zipfile.ZipFile('document.docx', 'r') as z:
content = z.read('word/document.xml').decode('utf-8')
# Strip XML tags
text = re.sub(r'<[^>]*>', '', content)
# Clean whitespace
lines = [line.strip() for line in text.split('\n') if line.strip()]
print('\n'.join(lines))
except Exception as e:
print(f'Error: {e}', file=sys.stderr)
sys.exit(1)
"
5. Save extracted text to file
# Try shell first
unzip -p document.docx word/document.xml 2>/dev/null | \
sed -e 's/<[^>]*>//g' > output.txt
# Verify output has content
if [ ! -s output.txt ]; then
# Fallback to Python
python3 -c "
import zipfile, re
with zipfile.ZipFile('document.docx', 'r') as z:
content = z.read('word/document.xml').decode('utf-8')
text = re.sub(r'<[^>]*>', '', content)
lines = [line.strip() for line in text.split('\n') if line.strip()]
print('\n'.join(lines))
" > output.txt
fi
Complete Shell Function with Fallback
Add this resilient function to your scripts:
parse_docx_resilient() {
local file="$1"
local output="$2"
if [ ! -f "$file" ]; then
echo "Error: File not found: $file" >&2
return 1
fi
# Primary: Shell extraction
local content
content=$(unzip -p "$file" word/document.xml 2>/dev/null | \
sed -e 's/<[^>]*>//g' | \
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | \
sed -e '/^$/d')
# Check if shell extraction succeeded
if [ -n "$content" ]; then
echo "$content" > "${output:-/dev/stdout}"
return 0
fi
# Fallback: Python zipfile
echo "Shell extraction failed, using Python fallback..." >&2
python3 -c "
import zipfile, sys, re
try:
with zipfile.ZipFile('$file', 'r') as z:
content = z.read('word/document.xml').decode('utf-8')
text = re.sub(r'<[^>]*>', '', content)
lines = [line.strip() for line in text.split('\n') if line.strip()]
print('\n'.join(lines))
except Exception as e:
print(f'Python extraction failed: {e}', file=sys.stderr)
sys.exit(1)
" > "${output:-/dev/stdout}" || return 1
}
# Usage examples:
# parse_docx_resilient document.docx # Output to stdout
# parse_docx_resilient document.docx out.txt # Output to file
Python Script Alternative
For complex workflows, save as a standalone script:
#!/usr/bin/env python3
"""DOCX text extractor with resilient fallback."""
import zipfile
import sys
import re
import subprocess
def extract_with_shell(filepath):
"""Try shell-based extraction first."""
try:
result = subprocess.run(
['unzip', '-p', filepath, 'word/document.xml'],
capture_output=True, text=True, timeout=10
)
if result.returncode == 0 and result.stdout.strip():
text = re.sub(r'<[^>]*>', '', result.stdout)
lin
常见问题
会先检查文件吗?
会。指南在提取文字之前,会先确认 Word 文件存在。
备用方案检查什么?
它检查 shell 提取是否返回了任何内容。
为什么使用 zipfile?
它是 Python 标准库的一部分,因此无需额外安装。
详情
作者的原始描述
Extract text from DOCX files with shell-primary approach and Python zipfile fallback for maximum reliability
repo: HKUDS/AgentPilot
热度
- GitHub 星标
- 7.8k
- 排名
- #12869
- 许可证
- 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帮你的智能体规划小型实验、阅读实验结果并记录发现,同时研究如何改进一个程序。