Word Document Manipulation with python-docx for Openclaw

A robust technical skill for automating Word document generation and template manipulation using python-docx while solving common XML run issues.

lnj22
v0.1.0
Apr 14, 2026
0
834
0

Install & Download

1. ClawHub CLI

The fastest way to install a skill directly from the registry.

npx clawhub@latest install offer-letter-generator-docx

2. Manual Installation

Copy the skill folder to one of these locations

Global
~/.openclaw/skills/
Workspace
<project>/skills/

Priority: Workspace > Local > Bundled

3. Prompt Installation

Copy this prompt to OpenClaw to install it automatically.

Help me install offer-letter-generator-docx using Clawhub. If Clawhub is not installed, install it first (npm i -g clawhub).

Prefer to download?

Get the raw skill files in a ZIP archive.

What is Word Document Manipulation with python-docx?

The docx skill provides a comprehensive framework for programmatic Word document manipulation. It specifically addresses the notorious split placeholder problem where Word divides template tags across multiple XML runs, rendering standard string replacement ineffective. By utilizing a paragraph-level search and rebuild strategy, this skill ensures reliable data injection into complex templates.

Beyond basic text replacement, this Openclaw Skills resource covers deep document structures including nested tables, headers, and footers. It provides developers with the logic needed to maintain document formatting while dynamically populating fields from JSON data sources, making it an essential tool for automated reporting and document generation workflows.

Word Document Manipulation with python-docx Use Cases

  • Generating automated business reports from JSON data structures.
  • Programmatically filling legal or HR templates with candidate and company information.
  • Cleaning up broken template placeholders caused by Word's internal XML run splitting.
  • Batch processing invoices or contracts that require nested table support and consistent headers/footers.
  • Implementing conditional logic within documents to include or exclude specific sections.

How Word Document Manipulation with python-docx Works

  1. The skill loads a .docx template and a corresponding JSON data file containing the replacement values.
  2. It scans the document at the paragraph level to identify placeholders formatted as {{KEY}}, bypassing the limitations of individual XML runs.
  3. A regex-based engine identifies all valid tags within main text, tables, and nested tables.
  4. The logic recurses through document sections to reach headers and footers that are typically isolated from the main body.
  5. Placeholders are replaced with actual data, and the paragraph is rebuilt using the first run's formatting to preserve the visual style.
  6. Conditional markers are processed to show or hide specific document fragments before the final document is saved.

Word Document Manipulation with python-docx Setup

To get started with this Openclaw Skills implementation, ensure you have the necessary library installed in your environment:

pip install python-docx

Prepare your template with placeholders like {{VARIABLE_NAME}} and ensure your data is structured in a standard JSON format for the fill_template function to process.

Word Document Manipulation with python-docx Data Schema & Taxonomy

The skill expects a mapping of uppercase keys to their corresponding string or numeric values. It also handles document structural data as follows:

Element Handling Method
Main Body Iterates through doc.paragraphs
Tables Recursive iteration through table.rows and cell.tables
Headers/Footers Accessed via doc.sections
Placeholders Regex pattern \\{\\{([A-Z_]+)\\}\\}
Conditionals Block markers {{IF_KEY}} and {{END_IF_KEY}}

Word Document Manipulation with python-docx Advanced Features

  • Robust Placeholder Recovery: Automatically fixes tags split by Word's internal formatting changes.
  • Deep Nesting Support: Recursively traverses tables within tables to ensure no data point is missed.
  • Formatting Preservation: Rebuilds paragraphs by clearing extra runs while maintaining the original font and style settings of the first run.
  • Global Scope Coverage: Reaches into every corner of the document including headers, footers, and all sections.
  • Conditional Logic: Supports basic templating logic to toggle the visibility of entire document sections based on data input.

SKILL.md


Loading

Related Openclaw Skills

METADATA

Github Stars: 0
forks: 0

Featured*