---
name: docx-shell-extract-049578ee1f4b
description: Extract text from DOCX files using shell commands when python-docx is unavailable
---

# DOCX Shell Extraction

## When to Use This Skill

Use this pattern when you need to read or extract text from Microsoft Word (.docx) files in constrained environments where:
- The `python-docx` library is not available
- You cannot install additional Python packages
- You need a quick, reliable shell-based solution

## Core Technique

DOCX files are ZIP archives containing XML files. The main document content is stored in `word/document.xml`. You can extract and parse this using standard shell tools.

## Step-by-Step Instructions

### Step 1: Extract the document.xml content

```bash
unzip -p filename.docx word/document.xml
```

The `-p` flag pipes the content to stdout without extracting to disk.

### Step 2: Strip XML tags to get plain text

```bash
unzip -p filename.docx word/document.xml | sed 's/<[^>]*>//g'
```

This removes all XML tags, leaving the text content.

### Step 3: Clean up whitespace (optional)

For cleaner output, add additional sed processing:

```bash
unzip -p filename.docx word/document.xml | \
  sed 's/<[^>]*>//g' | \
  sed 's/&[^;]*;//g' | \
  sed 's/^[[:space:]]*//' | \
  sed 's/[[:space:]]*$//' | \
  sed '/^$/d'
```

This removes:
- XML tags
- XML entities (like `&amp;`, `&lt;`)
- Leading/trailing whitespace
- Empty lines

### Step 4: Save to a text file (optional)

```bash
unzip -p filename.docx word/document.xml | \
  sed 's/<[^>]*>//g' > output.txt
```

## Complete Example

```bash
# Extract text from a Word document
DOCX_FILE="report.docx"
OUTPUT_FILE="report_text.txt"

unzip -p "$DOCX_FILE" word/document.xml | \
  sed 's/<[^>]*>//g' | \
  sed 's/&[^;]*;//g' | \
  sed '/^$/d' > "$OUTPUT_FILE"

echo "Extracted text saved to $OUTPUT_FILE"
```

## Verification

After extraction, verify the content was captured:

```bash
# Check if output file has content
if [ -s "$OUTPUT_FILE" ]; then
    echo "Successfully extracted $(wc -l < "$OUTPUT_FILE") lines"
    head -5 "$OUTPUT_FILE"
else
    echo "Warning: Output file is empty"
fi
```

## Limitations

- This method extracts raw text without formatting
- Complex layouts, tables, and images are not preserved
- Some special characters may need additional handling
- Works best for text-heavy documents

## Alternatives to Explore

If this approach fails or the DOCX structure differs:
- Check for `word/document.xml` existence: `unzip -l filename.docx | grep document.xml`
- Some documents may use `word/*.xml` with different naming
- Consider `pandoc` if available: `pandoc filename.docx -t plain`

---
Source attribution: AG064
Original project: https://github.com/AG064/OpenSpace/blob/ff671ef61ed6587c49bd54c81cdc751c0b99e363/gdpval_bench/skills/docx-shell-extract/SKILL.md
License: MIT
License information: https://opensource.org/license/mit
Original upstream instructions; downloaded from Mega AI Hub.

## Upstream license notice

MIT License

Copyright (c) 2026 ✨Data Intelligence Lab@HKU✨

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.



# Mega AI Hub resource metadata

Creator / publisher: AG064
Original project: https://github.com/AG064/OpenSpace/blob/ff671ef61ed6587c49bd54c81cdc751c0b99e363/gdpval_bench/skills/docx-shell-extract/SKILL.md
Attribution basis: Source repository record

Fields of work: Content & Media, Data & Analytics, Development & Engineering
Roles: Backend Developer, Data Analyst, Technical Writer
Specialties: API, Backend
Tools: Python Docx, Shell
Country applicability: Country not specified
Classification basis: Source catalog labels
Instruction SHA-256: 76f8661ebc51fa3fd0196fb4b3054fdbf52f159025e5d69d69af7c9dc47c13dd
