AIGC标识 【agent】qoder skill | docx-reader

 

SKILL.md

---
name: docx-reader
description: Read and extract text from .docx Word documents. Use when the user asks to read, view, summarize, or extract content from a .docx file. Triggers on requests involving .docx files, Word documents, or when a .docx path is provided.
---

DOCX Reader

Extract plain text from .docx files using the Python standard library.

Usage

When a .docx file needs to be read, run the bundled script:

python .qoder/skills/docx-reader/scripts/read_docx.py "<filepath>"

The script outputs the document's text content with UTF-8 encoding. Paragraphs are separated by newlines.

Notes

  • Uses only zipfile and xml.etree.ElementTree — no third-party dependencies required.
  • Extracts text from the main document body (word/document.xml) only. Headers, footers, text boxes, and embedded objects are not included.
  • If the output is garbled, ensure the terminal supports UTF-8 or redirect output to a file.

scripts/read_docx.py

"""Extract plain text from a .docx file using only Python standard library."""
import sys
import zipfile
import xml.etree.ElementTree as ET

NS = '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}'


def extract_text(filepath: str) -> str:
    paragraphs = []
    with zipfile.ZipFile(filepath) as z:
        xml_content = z.read('word/document.xml')
        tree = ET.fromstring(xml_content)
        for p in tree.iter(f'{NS}p'):
            texts = []
            for t in p.iter(f'{NS}t'):
                if t.text:
                    texts.append(t.text)
            if texts:
                paragraphs.append(''.join(texts))
    return '\n'.join(paragraphs)


if __name__ == '__main__':
    if len(sys.argv) < 2:
        print('Usage: python read_docx.py <filepath>', file=sys.stderr)
        sys.exit(1)
    sys.stdout.reconfigure(encoding='utf-8')
    print(extract_text(sys.argv[1]))

posted on 2026-08-07 11:34  fox_charon  阅读(11)  评论(0)    收藏  举报

导航