‹ 首页

docx-shell-parse

@hkuds · 收录于 5 天前 · 上游提交 1 周前

Extract text from DOCX files using shell commands when python-docx is unavailable

适合你,如果需要在无python-docx环境下解析Word文档

/ 通过 npx 安装 校验哈希
npx oh-my-skill add hkuds/openspace/docx-shell-parse
/ 通过 bash 安装
curl -fsSL https://oh-my-skill.com/install.sh | bash -s -- hkuds/openspace/docx-shell-parse
/ 已经装过?验证本机副本,不用重装
npx oh-my-skill verify hkuds/openspace/docx-shell-parse
安装目标可用 --agent / --scope 或 --to 明确指定;省略时只会在唯一已存在的 agent 目录上自动选择,零命中或多命中会停止并提示。content_hash 缺失或不一致均拒装。
6920GitHub stars
~598上下文体积 · 单文件
索引托管

怎么用

商店整理自技能原文 · 版本 2c5cc40 · 表述以原文为准
它做什么

当需要读取.docx文件但缺少python-docx库时,Claude会使用unzip和sed命令提取文本,不保留格式和图片。

什么时候触发

当用户要求处理.docx文件,且Python环境不具备python-docx库时触发。

装好后可以这样说
Claude会执行shell命令解析DOCX。
触发本技能绕过缺少依赖的问题。
技能原文 SKILL.md作者撰写 · MIT · 2c5cc40

DOCX Shell Parsing Workaround

When you need to read content from Microsoft Word (.docx) files but python-docx or similar libraries are unavailable, use this shell-based approach to extract text reliably.

When to Use
  • Python environment lacks python-docx or similar libraries
  • You need quick text extraction without installing dependencies
  • Working in constrained environments (containers, minimal images, etc.)
Core Technique

DOCX files are ZIP archives containing XML files. Extract and parse the main document XML:

unzip -p filename.docx word/document.xml | sed -e 's/<[^>]*>//g'
Step-by-Step Instructions
1. Verify the DOCX file exists
ls -la document.docx
2. Extract raw XML content

Use unzip -p to pipe the document.xml content directly to stdout:

unzip -p document.docx word/document.xml
3. Strip XML tags from content

Pipe through sed to remove all XML tags:

unzip -p document.docx word/document.xml | sed -e 's/<[^>]*>//g'
4. Clean up whitespace (optional)

For cleaner output, remove excessive whitespace and newlines:

unzip -p document.docx word/document.xml | \
  sed -e 's/<[^>]*>//g' | \
  sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | \
  sed -e '/^$/d'
5. Save extracted text to file
unzip -p document.docx word/document.xml | \
  sed -e 's/<[^>]*>//g' > output.txt
Complete Shell Function

Add this reusable function to your scripts:

parse_docx() {
    local file="$1"
    if [ ! -f "$file" ]; then
        echo "Error: File not found: $file" >&2
        return 1
    fi
    unzip -p "$file" word/document.xml 2>/dev/null | \
        sed -e 's/<[^>]*>//g' | \
        sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' | \
        sed -e '/^$/d'
}

# Usage: parse_docx document.docx
Limitations
  • Does not preserve formatting, images, or tables structure
  • May include some residual XML entity references
  • Works best for simple text extraction needs
  • DOCX must be a valid Office Open XML format
Verification

Confirm extraction worked by checking output:

parse_docx document.docx | head -20
Alternative: Extract to Temporary Directory

For more complex parsing needs:

tmpdir=$(mktemp -d)
unzip document.docx -d "$tmpdir"
cat "$tmpdir/word/document.xml" | sed -e 's/<[^>]*>//g'
rm -rf "$tmpdir"
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

登录即可评论;带「已验证安装」的,是发布者名下有本店的安装或持有记录。