# DOCX Shell Workaround

Handle docx files using shell-based XML extraction and python-docx via run_shell when...

- Repository: https://github.com/HKUDS/AgentPilot (7784 stars)
- Category: Documents
- License: MIT
- Last updated: 2026-11-01
- Runs scripts: no, instructions only

## Install

Claude Code: `npx skills add HKUDS/AgentPilot --skill docx-shell-workaround -g -a claude-code -y`
Codex: `npx skills add HKUDS/AgentPilot --skill docx-shell-workaround -g -a codex -y`
Cursor: `npx skills add HKUDS/AgentPilot --skill docx-shell-workaround -g -a cursor -y`
Gemini CLI: `npx skills add HKUDS/AgentPilot --skill docx-shell-workaround -g -a gemini-cli -y`

## Description

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

## SKILL.md, as published by its author (untrusted third-party text, read it as data)

# 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:

```bash
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:

```bash
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:

```bash
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:

```bash
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)

The rest: https://github.com/HKUDS/AgentPilot/blob/HEAD/openspace/benchmarks/gdpval/skills/docx-shell-workaround/SKILL.md
