‹ 首页

docx-shell-workaround

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

Handle docx files using shell-based XML extraction and python-docx via run_shell when standard tools fail

适合你,如果标准工具无法处理你的 docx 文件

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

怎么用

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

当标准工具无法处理 .docx 文件时,Claude 会改用 shell 命令解压文档并解析 XML 提取文本,或使用 python-docx 创建/修改文档。

什么时候触发

当 read_file 无法提取内容,或 execute_code_sandbox 创建/修改 docx 失败时触发。

装好后可以这样说
Claude 会用 python-docx 生成文档。
技能原文 SKILL.md作者撰写 · MIT · 2c5cc40

DOCX Shell Workaround

When to Use

Use this skill when:

  • read_file cannot extract content from .docx files
  • execute_code_sandbox encounters failures when creating or modifying docx files
  • You need a reliable fallback for docx file manipulation
Reading DOCX Files
Step 1: Extract document.xml using unzip

Use run_shell to unzip the .docx file (which is a ZIP archive) and extract the main document XML:

unzip -p input.docx word/document.xml > document.xml
Step 2: Parse XML with ElementTree via run_shell

Extract text content by parsing the XML. Use run_shell to execute Python code:

python3 << 'EOF'
import xml.etree.ElementTree as ET

tree = ET.parse('document.xml')
root = tree.getroot()
namespace = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

text_content = []
for elem in root.iter():
    if elem.tag.endswith('t'):
        if elem.text:
            text_content.append(elem.text)

text = ''.join(text_content)
print(text)
EOF
Step 3: Optional - More robust XML parsing

For better text extraction that handles paragraphs:

python3 << 'EOF'
import xml.etree.ElementTree as ET

tree = ET.parse('document.xml')
root = tree.getroot()
ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

paragraphs = []
for p in root.findall('.//w:p', ns):
    para_text = []
    for t in p.findall('.//w:t', ns):
        if t.text:
            para_text.append(t.text)
    if para_text:
        paragraphs.append(''.join(para_text))

for para in paragraphs:
    print(para)
EOF
Creating DOCX Files
Use python-docx via run_shell (NOT execute_code_sandbox)

When execute_code_sandbox fails for docx operations, use run_shell instead:

python3 << 'EOF'
from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH

doc = Document()

# Add heading
doc.add_heading('Document Title', 0)

# Add paragraph
doc.add_paragraph('Your content here.')

# Add section with heading
doc.add_heading('Section Title', level=1)
doc.add_paragraph('Section content with multiple paragraphs.')

# Add table if needed
table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'

# Save document
doc.save('output.docx')
print("Document created successfully")
EOF
Example: Creating a structured business document
python3 << 'EOF'
from docx import Document
from docx.shared import Pt

doc = Document()

# Title
title = doc.add_heading('Business Strategy Memo', 0)
title.alignment = 1  # Center

# Executive Summary
doc.add_heading('Executive Summary', level=1)
doc.add_paragraph('Brief overview of key points and recommendations.')

# Market Overview
doc.add_heading('Market Overview', level=1)
doc.add_paragraph('Analysis of current market conditions and trends.')

# Recommendations
doc.add_heading('Recommendations', level=1)
doc.add_paragraph('Actionable recommendations based on analysis.')

doc.save('Strategy_Memo.docx')
EOF
Full Workflow Example

Complete example showing extraction and creation:

# Step 1: Extract content from existing docx
unzip -p existing.docx word/document.xml > doc.xml

# Step 2: Parse and transform content
python3 << 'EOF'
import xml.etree.ElementTree as ET

tree = ET.parse('doc.xml')
root = tree.getroot()
ns = {'w': 'http://schemas.openxmlformats.org/wordprocessingml/2006/main'}

content = []
for p in root.findall('.//w:p', ns):
    para_text = []
    for t in p.findall('.//w:t', ns):
        if t.text:
            para_text.append(t.text)
    if para_text:
        content.append(''.join(para_text))

# Write extracted content to file for reference
with open('extracted.txt', 'w') as f:
    for line in content:
        f.write(line + '\n')
EOF

# Step 3: Create new docx with modified content
python3 << 'EOF'
from docx import Document

doc = Document()
doc.add_heading('Updated Document', 0)

with open('extracted.txt', 'r') as f:
    for line in f:
        if line.strip():
            doc.add_paragraph(line.strip())

doc.save('updated.docx')
EOF
Key Points
  1. Always use run_shell - Not execute_code_sandbox for docx operations
  2. DOCX is a ZIP archive - Contains XML files including word/document.xml
  3. Use unzip -p - The -p flag outputs to stdout, useful for piping
  4. Handle XML namespaces - Word XML uses the w: namespace prefix
  5. Install python-docx if needed - Run pip install python-docx in the shell if not available
Troubleshooting

If python-docx is not installed:

pip install python-docx

If unzip is not available:

# Use Python's zipfile module instead
python3 -c "import zipfile; z=zipfile.ZipFile('file.docx'); print(z.read('word/document.xml').decode('utf-8', errors='ignore'))"

If XML parsing fails:

  • Check the namespace URI matches your document version
  • Use .endswith('t') instead of full namespace matching for flexibility
  • Handle encoding issues with errors='ignore' parameter
按 MIT 许可原样转载,未经改动 · 在 GitHub 查看 →

评论

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