跳到正文
Skillpedia
PDF、Word 与幻灯片7.8k · #12870

docx-shell-parse

它能做什么

这项技能展示使用标准 shell 工具读取 .docx 文件的基本步骤。它适合在设置有限的环境中,需要快速获取 Word 文件文字的人。

适用场景

  • ✓当我电脑上没有安装用于 Word 文件的 Python 库的时候。
  • ✓当我需要快速提取文字,又不想安装依赖项的时候。
  • ✓当我在容器或工具很少的简化系统中工作的时候。

技能链接

https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/docx-shell-parse/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/HKUDS/AgentPilot/blob/main/openspace/benchmarks/gdpval/skills/docx-shell-parse/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我获取这份 Word 文档的文字。这里无法安装库,请用 shell 完成。”

每周 5 个新技能,1 分钟读完。

工作原理

AI 会确认文件存在,解压主文档 XML,去除标签和空行,并可以把文字保存到文件中。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

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"
常见问题

Word 文件的哪一部分保存了文字?

主要内容位于压缩包内的 word/document.xml 文件中。

空行会被删除吗?

可选的清理步骤会去除多余的空白和空行。

我可以保存结果吗?

可以。指南展示了如何把提取的文字写入文本文件。

详情

作者的原始描述

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

repo: HKUDS/AgentPilot

热度

GitHub 星标
7.8k
排名
#12870
许可证
MIT
更新时间
2026/11/1

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能