docx-parse-resilient
Skill này làm gì
Skill này là cách hai bước để đọc tệp .docx, trong đó một phương án Python dự phòng được dùng khi cách shell thất bại. Dành cho người làm trong container hoặc môi trường tối giản, nơi công cụ có sẵn khác nhau.
Khi nào nên dùng
- ✓Khi lệnh unzip trả về lỗi hoặc không có đầu ra với tệp Word.
- ✓Khi tôi làm việc trong container, tác vụ CI hoặc môi trường tối giản với ít công cụ.
- ✓Khi tôi cần trích xuất văn bản Word đáng tin cậy và tự chuyển sang phương án dự phòng.
Liên kết skill
https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/docx-shell-parse-enhanced/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Hãy trích xuất văn bản từ tệp Word này. Nếu lệnh unzip không cho kết quả, hãy chuyển sang Python và thử lại.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
AI chạy trích xuất shell trước, kiểm tra xem có văn bản nào được trả về không, và nếu kết quả trống thì đọc XML của tệp Word bằng mô-đun zipfile của Python.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
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
Câu hỏi thường gặp
Tệp có được kiểm tra trước không?
Có. Hướng dẫn bắt đầu bằng việc xác nhận tệp Word tồn tại trước khi trích xuất văn bản.
Phương án dự phòng kiểm tra gì?
Nó kiểm tra xem kết quả trích xuất bằng shell có nội dung nào không.
Vì sao dùng zipfile?
Nó là một phần của thư viện chuẩn Python, nên không cần cài thêm.
Chi tiết
Mô tả gốc của tác giả
Extract text from DOCX files with shell-primary approach and Python zipfile fallback for maximum reliability
repo: HKUDS/AgentPilot
Độ phổ biến
- Sao GitHub
- 7.8k
- Vị trí xếp hạng
- #12869
- Giấy phép
- MIT
- Cập nhật
- 1/11/2026
Bình luận
- …
Thêm skill từ kho mã này
agentpilot
7.8kGiúp agent của bạn cải thiện dự án từng bước bằng cách chuẩn bị tác vụ và chạy vòng lặp AgentPilot, gồm đề xuất, kiểm thử và chấm điểm thay đổi.
openspace
7.8kGiúp agent của bạn thiết lập OpenSpace, công cụ lưu và lấy lại skill của agent, rồi kết nối nó với ứng dụng AI của bạn qua MCP.
rsi-arena
7.8kGiúp agent của bạn thiết lập và chạy các bài kiểm thử benchmark AI chuẩn, như AIME và Terminal-Bench, để so sánh các phương pháp cải tiến khác nhau.
experiment-guide
7.8kGiúp agent của bạn lên kế hoạch các thí nghiệm nhỏ, đọc kết quả và ghi lại phát hiện khi tìm cách cải thiện một chương trình.
Skill tương tự
feishu-doc
394kGiúp agent của bạn đọc, tạo và sửa tài liệu đám mây Feishu, gồm văn bản, hình ảnh và bảng, bằng công cụ tài liệu.
feishu-drive
394kCho phép agent của bạn duyệt, sắp xếp và bình luận các file trong một thư mục đám mây Feishu đã được chia sẻ với bot.
feishu-perm
394kGiúp agent của bạn kiểm tra và thay đổi ai có quyền truy cập một file Feishu, chọn quyền nhỏ nhất đáp ứng yêu cầu của bạn.