Python-based tools for manipulating Microsoft Word documents programmatically.

Install

mkdir -p .claude/skills/docx-jnpiyush && curl -L -o skill.zip "https://agentskills.codes/api/skills/download/10676" && unzip -o skill.zip -d .claude/skills/docx-jnpiyush && rm skill.zip

Installs to .claude/skills/docx-jnpiyush

Activation

This is the description your AI agent reads to decide when to run this skill — the better it matches your request, the more reliably it fires.

Read, write, and transform Microsoft Word .docx files. Use when extracting text or tables from Word documents, generating reports from templates, applying styles, inserting images, building tables, or converting Markdown/HTML to Word.
234 chars✓ has a “when” trigger
Intermediate

Key capabilities

  • Read Word documents
  • Write Word documents
  • Fill Word templates
  • Convert Markdown to Word

How it works

Uses Python libraries to read, write, and template Microsoft Word .docx files.

Inputs & outputs

You give it
Word document or template
You get back
Transformed Word document

When to use docx

  • Generate word report
  • Extract tables from docx
  • Fill word template
  • Convert markdown to word

About this skill

DOCX

Practical patterns for reading, writing, and templating Microsoft Word .docx files.

Prerequisites

  • Python 3.9+
  • Pick libraries by task:
    • python-docx -- read/write paragraphs, runs, tables, sections, headers/footers
    • docxtpl -- Jinja2-style templating on top of a designer-authored .docx
    • mammoth -- convert .docx -> clean HTML or Markdown (drops most styling)
    • pandoc (CLI) -- convert between Markdown / HTML / .docx reliably

When to Use

  • Input or output of the task is a .docx file
  • You need to generate a Word report (proposals, contracts, briefs)
  • You need to extract text or tables from Word for downstream processing
  • You need to merge a data row set into a designer-authored template

Library Selection

GoalPreferNotes
Generate from scratchpython-docxFull control over paragraphs, runs, tables
Generate from a designed templatedocxtplDesigner controls layout; code fills placeholders
Convert .docx -> Markdown/HTMLmammoth or pandocmammoth preserves semantic mapping, pandoc is more general
Convert Markdown -> .docxpandocUse a reference doc for styles
Bulk read for search/indexingpython-docx or mammothSkip styling, grab plain text
Legacy .doc (binary)pandoc (via LibreOffice)python-docx does NOT support .doc

Read Paragraphs and Tables

from docx import Document

def read_text(path: str) -> str:
    doc = Document(path)
    return "\n".join(p.text for p in doc.paragraphs if p.text)

def read_tables(path: str) -> list[list[list[str]]]:
    doc = Document(path)
    return [
        [[cell.text for cell in row.cells] for row in table.rows]
        for table in doc.tables
    ]

Notes:

  • doc.paragraphs returns body paragraphs only; headers/footers and table cells are accessed separately.
  • For table cells, iterate table.rows -> row.cells. A cell may itself contain paragraphs and nested tables.

Read Headers, Footers, and Sections

from docx import Document

doc = Document("input.docx")
for section in doc.sections:
    header = section.header
    footer = section.footer
    for paragraph in header.paragraphs:
        print("HEADER:", paragraph.text)
    for paragraph in footer.paragraphs:
        print("FOOTER:", paragraph.text)

Create a New Document

from docx import Document
from docx.shared import Inches, Pt

doc = Document()

title = doc.add_heading("Quarterly Report", level=1)

p = doc.add_paragraph("Summary of the period. ")
run = p.add_run("Important text.")
run.bold = True
run.font.size = Pt(12)

doc.add_paragraph("Bullet one", style="List Bullet")
doc.add_paragraph("Bullet two", style="List Bullet")

table = doc.add_table(rows=1, cols=3)
table.style = "Light Grid Accent 1"
hdr = table.rows[0].cells
hdr[0].text = "Metric"
hdr[1].text = "Q1"
hdr[2].text = "Q2"
row = table.add_row().cells
row[0].text = "Revenue"
row[1].text = "100"
row[2].text = "120"

doc.add_picture("chart.png", width=Inches(4))

doc.save("report.docx")

Style names must already exist in the document. To use custom styles, start from a template .docx that defines them.

Use a Template (docxtpl)

Designers author the layout in Word with placeholders like {{ customer_name }} and {% for item in items %}...

from docxtpl import DocxTemplate

def render(template_path: str, output_path: str, context: dict) -> None:
    tpl = DocxTemplate(template_path)
    tpl.render(context)
    tpl.save(output_path)

render(
    "proposal-template.docx",
    "proposal-acme.docx",
    {
        "customer_name": "Acme Corp",
        "items": [
            {"name": "Setup", "price": 1000},
            {"name": "Support", "price": 500},
        ],
        "total": 1500,
    },
)

This keeps formatting decisions in the document, not in code.

Modify an Existing Document

Search-and-replace must walk the document tree, because Word splits styled text across multiple run elements inside a paragraph:

from docx import Document

def replace_in_paragraph(paragraph, find: str, replace: str) -> None:
    if find in paragraph.text:
        # Join runs, replace, write back into the first run, clear the rest.
        merged = "".join(run.text for run in paragraph.runs).replace(find, replace)
        if paragraph.runs:
            paragraph.runs[0].text = merged
            for run in paragraph.runs[1:]:
                run.text = ""

def replace_everywhere(path: str, output: str, mapping: dict[str, str]) -> None:
    doc = Document(path)
    for paragraph in doc.paragraphs:
        for k, v in mapping.items():
            replace_in_paragraph(paragraph, k, v)
    for table in doc.tables:
        for row in table.rows:
            for cell in row.cells:
                for paragraph in cell.paragraphs:
                    for k, v in mapping.items():
                        replace_in_paragraph(paragraph, k, v)
    doc.save(output)

This naive merge loses per-run formatting inside the matched span. For richer edits, prefer docxtpl placeholders.

Convert .docx <-> Markdown / HTML

import mammoth

def docx_to_html(path: str) -> str:
    with open(path, "rb") as f:
        result = mammoth.convert_to_html(f)
    return result.value  # HTML

def docx_to_markdown(path: str) -> str:
    with open(path, "rb") as f:
        result = mammoth.convert_to_markdown(f)
    return result.value

CLI (best for round-trips and Markdown -> Word):

pandoc report.md -o report.docx --reference-doc=reference.docx
pandoc report.docx -o report.md --extract-media=./media

A reference doc carries your styles (fonts, headings, colors). Build one by saving a styled Word doc and pointing --reference-doc at it.

Inspect or Repair a Corrupt File

.docx is a ZIP of XML parts. To inspect:

unzip -l file.docx
unzip -p file.docx word/document.xml | head

If Word refuses to open a programmatically built file, common causes:

  • Invalid XML written by hand-edited tools -- validate the word/document.xml part.
  • Missing [Content_Types].xml entry for a new image type.
  • Image referenced in XML but missing from the word/media/ folder.

Use python-docx (which manages the package for you) or pandoc rather than hand-editing the ZIP.

Common Errors

  • PackageNotFoundError -- the file is not a .docx (often legacy .doc); convert with pandoc or LibreOffice first.
  • Text appears but search-and-replace finds nothing -- the match is split across runs; merge runs (see above) or template instead.
  • Images do not render -- ensure the file extension matches the binary content; embed via add_picture, not by editing XML.
  • Style not applied -- the style name does not exist in the source template; copy from a known-good document.

Done Criteria

  • Library choice matches the task per the table above
  • Generated documents open in Microsoft Word, LibreOffice Writer, and Google Docs
  • Templates and reference docs live under source control, not in user data folders
  • Secrets and customer data are not committed to template files
  • Conversions preserve at least: headings, lists, tables, images, links

When not to use it

  • Legacy .doc files
  • Hand-editing XML

Prerequisites

Python

Limitations

  • No support for legacy .doc
  • Requires template for styles

How it compares

Provides programmatic control over Word documents instead of manual editing.

Compared to similar skills

docx side by side with the closest alternatives in the catalog.

SkillInstallsUpdatedSafetyDifficulty
docx (this skill)02moReviewIntermediate
business-document-generator39moReviewBeginner
xhs-note-creator44moReviewBeginner
pr-writing-review22moReviewBeginner

Try saying

Example prompts that trigger this skill in your AI assistant.

More by jnPiyush

View all by jnPiyush

ux-ui-design

jnPiyush

Design user experiences with wireframing, prototyping, user flows, accessibility, and production-ready HTML prototypes. Use when creating wireframes, building interactive prototypes, designing user flows, implementing accessibility standards, or producing HTML/CSS design deliverables.

00

copilot-studio-agents

jnPiyush

Design Microsoft Copilot Studio agents (formerly Power Virtual Agents) -- topics, trigger phrases, generative answers, knowledge sources, connector and MCP actions, authentication, channels, and agent flows -- so an agent can author the conversational logic that ships as a Bot component inside a Pow

00

verification-before-completion

jnPiyush

Block false completion claims. Force the agent to identify the claim, run the exact verification command, read the actual output, compare against the claim, and only then report. Use whenever an agent is about to say "done", "fixed", "tests pass", "deployed", "loop complete", or close an issue.

00

configuration

jnPiyush

Implement configuration management patterns including environment variables, secrets, feature flags, and validation strategies. Use when setting up app configuration, managing environment-specific settings, implementing feature flags, storing secrets securely, or validating configuration at startup.

00

error-handling

jnPiyush

Implement robust error handling with exceptions, retry logic, circuit breakers, and graceful degradation. Use when designing error handling strategies, implementing retry policies, adding circuit breakers, configuring timeouts, or building health check endpoints.

00

mcp-apps-development

jnPiyush

Build MCP Apps (ext-apps) that render interactive UI inside conversational AI clients. Use when creating visual tool outputs, interactive dashboards, form-based tools, or rich media experiences in MCP-compatible hosts like Claude Desktop, VS Code Copilot Chat, or other MCP clients that support the A

00

You might also like

business-document-generator

ailabs-393

This skill should be used when the user requests to create professional business documents (proposals, business plans, or budgets) from templates. It provides PDF templates and a Python script for generating filled documents from user data.

316

xhs-note-creator

comeonzhj

小红书笔记素材创作技能。当用户需要创建小红书笔记素材时使用这个技能。技能包含:根据用户的需求和提供的资料,撰写小红书笔记内容(标题+正文),生成图片卡片(封面+正文卡片),以及发布小红书笔记。

49

pr-writing-review

evalstate

Extract and analyze writing improvements from GitHub PR review comments. Use when asked to show review feedback, style changes, or editorial improvements from a GitHub pull request URL. Handles both explicit suggestions and plain text feedback. Produces structured output comparing original phrasing with reviewer suggestions to help refine future writing.

27

google-docs-skill

javimosch

Direct access to the Google Docs API using OAuth 2.0. Create documents, insert and format text, and manage document content.

00

voz

sbroggioadv

Aprende o estilo de escrita da compradora analisando mensagens que ela mesma enviou no WhatsApp e gera o `voz.md` consumido pelo agente `redator`. Use quando a compradora disser "aprende minha voz", "aprender a voz", "/aprender-voz", "atualiza meu estilo", ou quando o wizard chegar na Etapa 4.

00

documentation-specialist

PedroFoll

Especialista em documentação de projetos Django/Python. Gera README.md completo em português, documenta arquitetura, decisões técnicas, instruções de instalação, uso e testes para repositórios GitHub.

00

Search skills

Search the agent skills registry