docx-shell-extract
Extract text from DOCX files using shell commands when python-docx is unavailable
- License 许可
- MIT
- 最近更新
- 2026-04-06
- Stars
- ★ 0
- Forks
- 0
01 前言 Front Matter
- name
- docx-shell-extract
- description
- Extract text from DOCX files using shell commands when python-docx is unavailable
02 资源文件 1
- 其他.skill_id32 B
03 SKILL.md 正文
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
04 同仓库更多 Skills
zip-export-deliverable
Ensures codebase deliverables are properly exported as ZIP archives through a dedicated finalization step
zip-deliverable-finalization
Ensure codebase deliverables are properly packaged as ZIP exports in a dedicated final step
zero-iteration-failure-analysis
Identify and handle agent failures with 0 iterations as pre-execution system issues
write-file-fallback-report
Generate professional documents using write_file when primary data sources fail, leveraging embedded domain knowledge instead of external retrieval
fallback-doc-with-delegation
Generate professional documents after tool failures using shell_agent for complex formats or write_file for simple text/markdown
context-anchored-fallback-report
Generate documents with write_file when retrieval tools fail, with explicit guardrails against task context drift