Skip to content
Skillpedia
PDF, Word & slides54k Β· #3368

docx

What it does

This skill lets an AI agent work with Microsoft Word documents. It is for people who need reports, letters, or memos written as Word files, or existing Word files read and edited.

When to use it

  • βœ“When you need a Word report, letter, or memo made as a .docx file.
  • βœ“When you want the text or tables pulled out of an existing Word file.
  • βœ“When you need tracked changes or comments added to a Word document.

Skill link

https://github.com/HKUDS/DeepTutor/blob/main/deeptutor/skills/builtin/docx/SKILL.md

Copy it and paste it into your AI (Claude, ChatGPT or Cursor), asking it to follow the instructions.

How to use

The easiest way: copy the message and paste it into your AI. It reads the skill and follows the instructions.

Ready-to-paste message

Use this skill to help me: https://github.com/HKUDS/DeepTutor/blob/main/deeptutor/skills/builtin/docx/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
Want your AI to find skills like this on its own?Install the free Skillpedia MCP: one line, and it searches 15,000+ skills before each task.Install in 1 minute β†’

Example of what to ask your AI

β€œTurn these notes into a Word report with headings, a table, and page numbers.”

5 new skills a week. 1 minute to read.

How it works

The agent uses the python-docx library for most tasks, and writes raw Word XML for advanced cases such as tracked changes and comments. It saves a new output file rather than overwriting the original.

Technical content (original SKILL.md)

Original text by the author, straight from GitHub.

DOCX (Microsoft Word)

A .docx is a ZIP of XML parts. Body text lives in word/document.xml. Two tiers:

  • Default β€” python-docx (preinstalled): create, read, and simple edits. Use for almost everything.
  • Advanced β€” raw OOXML via zipfile: only for what python-docx cannot express β€” tracked changes (redlines), comments, and exact-fidelity edits that must preserve every untouched byte. See Raw OOXML.

Runtime

Use exec with complete Python source (language: python). python-docx is preinstalled. Prefer creating, saving, reopening, and validating the deliverable in one call; later calls can revise the same relative filename. Follow the turn's User workspace instructions for locating inputs, output boundaries, and presenting the finished file. When editing, write a new output unless the user explicitly requested an authorized replacement.

Read / extract

from docx import Document

doc = Document("in.docx")
text = "\n".join(p.text for p in doc.paragraphs)  # body paragraphs
for tbl in doc.tables:  # tables
    for row in tbl.rows:
        print([c.text for c in row.cells])

doc.paragraphs skips text inside tables, headers/footers, and text boxes β€” iterate doc.tables and doc.sections[i].header/.footer for those. Each paragraph's style: p.style.name (e.g. "Heading 1").

To read tracked changes, parse the XML directly β€” python-docx ignores <w:ins>/<w:del>:

import zipfile, re

xml = zipfile.ZipFile("in.docx").read("word/document.xml").decode("utf-8")
# inserted text = <w:ins>…<w:t>…  deleted = <w:del>…<w:delText>…
print(re.findall(r"<w:t[^>]*>(.*?)</w:t>", xml))

Create

from docx import Document
from docx.shared import Pt, Inches, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH

doc = Document()  # default template page size
doc.add_heading("Quarterly Report", level=0)  # 0 = title; 1..9 = H1..H9
p = doc.add_paragraph("Intro paragraph. ")
run = p.add_run("Bold tail.")
run.bold = True
doc.add_paragraph("First item", style="List Bullet")  # real list style, never a "β€’ " literal
doc.add_paragraph("Step one", style="List Number")

# Table β€” header row + data
tbl = doc.add_table(rows=1, cols=2)
tbl.style = "Light Grid Accent 1"
tbl.rows[0].cells[0].text, tbl.rows[0].cells[1].text = "Metric", "Value"
for k, v in [("Revenue", "1.2M"), ("Growth", "15%")]:
    c = tbl.add_row().cells
    c[0].text, c[1].text = k, v

doc.add_picture("chart.png", width=Inches(5))  # image, scaled to width
doc.add_page_break()
doc.save("out.docx")

# Validate immediately; later exec calls can also reopen this relative path.
check = Document("out.docx")
assert check.paragraphs, "generated DOCX has no paragraphs"
import zipfile

with zipfile.ZipFile("out.docx") as package:
    assert package.testzip() is None, "generated DOCX has a corrupt ZIP member"

Rules:

  • Never type bullet/number characters (β€’, 1.) into text β€” use style="List Bullet"/"List Number". Only list styles defined in the doc's template are available.
  • No \n inside a run β€” each visual line is its own add_paragraph.
  • Built-in style names must match the template (e.g. "Heading 1", "List Bullet"); a wrong name raises KeyError.
  • Units: Pt, Inches, Cm from docx.shared. Colors: RGBColor(0x1F,0x4E,0x79).

Page setup, headers/footers, page numbers

from docx.shared import Inches

sec = doc.sections[0]
sec.page_width, sec.page_height = Inches(8.5), Inches(11)  # Letter
sec.top_margin = sec.bottom_margin = Inches(1)
sec.header.paragraphs[0].text = "Confidential"

Page-number fields aren't in the python-docx API; inject the field XML into a footer run:

from docx.oxml.ns import qn
from docx.oxml import OxmlElement


def add_page_number(paragraph):
    for t in ("begin", "instr", "end"):
        r = OxmlElement("w:r")
        if t == "instr":
            fld = OxmlElement("w:instrText")
            fld.set(qn("xml:space"), "preserve")
            fld.text = "PAGE"
        else:
            fld = OxmlElement("w:fldChar")
            fld.set(qn("w:fldCharType"), t)
        r.append(fld)
        paragraph._p.append(r)


add_page_number(doc.sections[0].footer.paragraphs[0])

A clickable Table of Contents is also a field; Word shows "right-click β†’ Update Field" until refreshed. Same pattern with instrText = TOC \o "1-3" \h \z \u.

Edit existing (simple)

python-docx preserves the rest of the doc

FAQ

Does it work with PDF or Excel files?

No. The skill is for Word files, and it says PDF, Excel, PowerPoint, and Google Docs are outside its scope.

Can it add a table of contents?

Yes. The skill covers tables of contents and page numbers.

Can it make tracked changes?

Yes. It can apply tracked changes and comments through its advanced method.

Details

Original description by the author

Read, create, or edit Microsoft Word .docx files β€” extract/summarize text and tables, generate reports/letters/memos with headings, tables, images, TOC and page numbers, do find-and-replace, or apply tracked changes (redlines) and comments. Use whenever the user has a .docx or wants a Word deliverable. Not for PDF, .xlsx, .pptx, or Google Docs.

repo: HKUDS/DeepTutor

Popularity

GitHub stars
54k
Ranking position
#3368
License
Apache-2.0
Updated
10/31/2026

Is this skill useful?

0

Sign in to vote.

Comments

  • …

More skills from this repository

Similar skills