# Docx Shell Workaround

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

- **Type:** Skill
- **Install:** `agentstack add skill-hkuds-openspace-docx-shell-workaround`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [HKUDS](https://agentstack.voostack.com/s/hkuds)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [HKUDS](https://github.com/HKUDS)
- **Source:** https://github.com/HKUDS/OpenSpace/tree/main/gdpval_bench/skills/docx-shell-workaround

## Install

```sh
agentstack add skill-hkuds-openspace-docx-shell-workaround
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# 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  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:
```bash
pip install python-docx
```

If unzip is not available:
```bash
# 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

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [HKUDS](https://github.com/HKUDS)
- **Source:** [HKUDS/OpenSpace](https://github.com/HKUDS/OpenSpace)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-hkuds-openspace-docx-shell-workaround
- Seller: https://agentstack.voostack.com/s/hkuds
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
