Incremental audio production with duration mismatch handling, adaptive stem extension, and pre-mix alignment verification
日本語の概要は準備中です。原文の説明を表示しています。
Extract text from DOCX files with shell-primary approach and Python zipfile fallback for maximum reliability
インストール方法を見るインストールする前に、エージェントに与えられる指示の中身を確認できます。
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.
python-docx but zipfile module is available (standard library)unzip command returns errors or no outputDOCX files are ZIP archives containing XML files. This skill provides two extraction methods:
unzip -p + sed for fast extractionzipfile module for reliable extraction when shell failsls -la document.docx
Try the shell-based approach:
unzip -p document.docx word/document.xml 2>/dev/null | sed -e 's/<[^>]*>//g'
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
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)
"
# 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
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
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)
lines = [l.strip() for l in text.split('\n') if l.strip()]
return '\n'.join(lines)
except Exception:
pass
return None
def extract_with_python(filepath):
"""Fallback Python zipfile extraction."""
with zipfile.ZipFile(filepath, 'r') as z:
content = z.read('word/document.xml').decode('utf-8')
text = re.sub(r'<[^>]*>', '', content)
lines = [l.strip() for l in text.split('\n') if l.strip()]
return '\n'.join(lines)
def parse_docx_resilient(filepath):
"""Extract text with automatic fallback."""
# Try shell first
content = extract_with_shell(filepath)
if content:
return content, 'shell'
# Fallback to Python
content = extract_with_python(filepath)
return content, 'python'
if __name__ == '__main__':
if len(sys.argv) < 2:
print("Usage: parse_docx_resilient.py <file.docx>", file=sys.stderr)
sys.exit(1)
content, method = parse_docx_resilient(sys.argv[1])
if content:
print(f"# Extracted using {method} method", file=sys.stderr)
print(content)
else:
print("Failed to extract text from DOCX", file=sys.stderr)
sys.exit(1)
Confirm extraction worked by checking output:
# Test shell method
parse_docx_resilient document.docx | head -20
# Test with file output
parse_docx_resilient document.docx extracted.txt
wc -l extracted.txt # Should show line count > 0
# Verify content
grep -c "[a-zA-Z]" extracted.txt # Should show character content
Shell returns "unknown error" or no output:
which unzip to verify unzip is availablePython also fails:
file document.docxunzip -t document.docxpython3 --versionFile not found errors:
ls -la document.docxTo pre-detect which method to use:
# Check if unzip is available
if command -v unzip &> /dev/null; then
echo "Shell method available"
else
echo "Only Python method available"
fi
# Check if Python 3 is available
if command -v python3 &> /dev/null; then
echo "Python fallback available"
else
echo "Warning: No extraction method available!"
fi
まだレビューはありません。使ってみた感想をお寄せください。
概要と使いどころ
Incremental audio production with duration mismatch handling, adaptive stem extension, and pre-mix alignment verification
日本語の概要は準備中です。原文の説明を表示しています。
Incremental audio production with duration alignment handling, per-stem verification, and adaptive extension strategies
日本語の概要は準備中です。原文の説明を表示しています。
Create serverless API proxy endpoints that hide API keys and provide a unified backend for the dashboard frontend. Designed for Vercel deployment.
日本語の概要は準備中です。原文の説明を表示しています。
End-to-end audio production workflow with stems, effects, archiving, and verification
日本語の概要は準備中です。原文の説明を表示しています。
Handle cascading data retrieval tool failures by falling back to embedded knowledge generation
日本語の概要は準備中です。原文の説明を表示しています。
Fallback pattern for executing Python code when execute_code_sandbox fails
日本語の概要は準備中です。原文の説明を表示しています。