DOCX 文档操作:自动化 Word 文档创建 - Openclaw Skills

作者:互联网

2026-04-16

AI教程

什么是 DOCX 文档操作?

DOCX 文档操作技能允许 AI 智能体直接与 Microsoft Word 文件交互。通过利用 python-docx 库,此 Openclaw Skills 集成实现了无需人工干预即可生成专业报告、合同和信函。它提供了一个强大的框架来处理文档结构,包括节、样式以及表格和图像等复杂元素。

此技能专为需要弥补结构化数据与专业文档格式之间差距的开发人员和高级用户而设计。它通过自动化从创建到最终格式化的整个文档生命周期,消除了手动复制粘贴的需求。

下载入口:https://github.com/openclaw/skills/tree/main/skills/lijie420461340/docx-manipulation

安装与下载

1. ClawHub CLI

从源直接安装技能的最快方式。

npx clawhub@latest install docx-manipulation

2. 手动安装

将技能文件夹复制到以下位置之一

全局模式 ~/.openclaw/skills/ 工作区 /skills/

优先级:工作区 > 本地 > 内置

3. 提示词安装

将此提示词复制到 OpenClaw 即可自动安装。

请帮我使用 Clawhub 安装 docx-manipulation。如果尚未安装 Clawhub,请先安装(npm i -g clawhub)。

DOCX 文档操作 应用场景

  • 自动生成定期业务报告和销售摘要。
  • 以编程方式创建带有动态占位符的法律合同或协议。
  • 将结构化数据或 markdown 内容转换为精美的 Word 文档。
  • 为现有文档集添加一致的页眉、页脚和品牌标识。
  • 批量处理 Word 文档以更新样式或元数据。
DOCX 文档操作 工作原理
  1. 用户提供文档需求或所需修改的自然语言描述。
  2. 智能体处理任何提供的源文本、数据或图像。
  3. 生成一个利用 python-docx 库的 Python 脚本来执行特定的文档操作。
  4. 在安全环境中执行脚本以生成或更新 .docx 文件。
  5. 保存最终文档,并可通过 Openclaw Skills 进行下载或进一步处理。

DOCX 文档操作 配置指南

要使用此 Openclaw Skills 组件,请确保您的环境中安装了 python-docx 库:

pip install python-docx

DOCX 文档操作 数据架构与分类体系

该技能使用标准的 Office Open XML (.docx) 格式管理数据。它根据以下层次结构组织数据:

组件 描述
文档 所有内容和元数据的根容器。
定义页边距、方向和大小等页面属性。
段落 包含具有特定字符格式的运行(runs)的文本块。
表格 用于数据展示的网格结构,支持合并单元格。
图片 嵌入在段落或运行中的内联图像对象。
样式 用于标题、列表和表格的可重用格式定义。
name: docx-manipulation
description: Create, edit, and manipulate Word documents programmatically using python-docx
author: claude-office-skills
version: "1.0"
tags: [document, word, docx, python-docx, automation]
models: [claude-sonnet-4, claude-opus-4]
tools: [computer, code_execution, file_operations]
library:
  name: python-docx
  url: https://github.com/python-openxml/python-docx
  stars: 5.4k

DOCX Manipulation Skill

Overview

This skill enables programmatic creation, editing, and manipulation of Microsoft Word (.docx) documents using the python-docx library. Create professional documents with proper formatting, styles, tables, and images without manual editing.

How to Use

  1. Describe what you want to create or modify in a Word document
  2. Provide any source content (text, data, images)
  3. I'll generate python-docx code and execute it

Example prompts:

  • "Create a professional report with title, headings, and a table"
  • "Add a header and footer to this document"
  • "Generate a contract document with placeholders"
  • "Convert this markdown content to a styled Word document"

Domain Knowledge

python-docx Fundamentals

from docx import Document
from docx.shared import Inches, Pt, Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.style import WD_STYLE_TYPE

# Create new document
doc = Document()

# Or open existing
doc = Document('existing.docx')

Document Structure

Document
├── sections (margins, orientation, size)
├── paragraphs (text with formatting)
├── tables (rows, cells, merged cells)
├── pictures (inline images)
└── styles (predefined formatting)

Adding Content

Paragraphs & Headings

# Add heading (level 0-9)
doc.add_heading('Main Title', level=0)
doc.add_heading('Section Title', level=1)

# Add paragraph
para = doc.add_paragraph('Normal text here')

# Add styled paragraph
doc.add_paragraph('Note: Important!', style='Intense Quote')

# Add with inline formatting
para = doc.add_paragraph()
para.add_run('Bold text').bold = True
para.add_run(' and ')
para.add_run('italic text').italic = True

Tables

# Create table
table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'

# Add content
table.cell(0, 0).text = 'Header 1'
table.rows[0].cells[1].text = 'Header 2'

# Add row dynamically
row = table.add_row()
row.cells[0].text = 'New data'

# Merge cells
a = table.cell(0, 0)
b = table.cell(0, 2)
a.merge(b)

Images

# Add image with size
doc.add_picture('image.png', width=Inches(4))

# Add to specific paragraph
para = doc.add_paragraph()
run = para.add_run()
run.add_picture('logo.png', width=Inches(1.5))

Formatting

Paragraph Formatting

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

para = doc.add_paragraph('Formatted text')
para.alignment = WD_ALIGN_PARAGRAPH.CENTER
para.paragraph_format.line_spacing = 1.5
para.paragraph_format.space_after = Pt(12)
para.paragraph_format.first_line_indent = Inches(0.5)

Character Formatting

run = para.add_run('Styled text')
run.bold = True
run.italic = True
run.underline = True
run.font.name = 'Arial'
run.font.size = Pt(14)
run.font.color.rgb = RGBColor(0x00, 0x00, 0xFF)  # Blue

Page Setup

from docx.enum.section import WD_ORIENT
from docx.shared import Inches

section = doc.sections[0]
section.page_width = Inches(11)
section.page_height = Inches(8.5)
section.orientation = WD_ORIENT.LANDSCAPE
section.left_margin = Inches(1)
section.right_margin = Inches(1)

Headers & Footers

section = doc.sections[0]

# Header
header = section.header
header.paragraphs[0].text = "Company Name"
header.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER

# Footer with page numbers
footer = section.footer
para = footer.paragraphs[0]
para.text = "Page "
# Add page number field
run = para.add_run()
fldChar1 = OxmlElement('w:fldChar')
fldChar1.set(qn('w:fldCharType'), 'begin')
run._r.append(fldChar1)
# ... (field code for page number)

Styles

# Use built-in styles
doc.add_paragraph('Heading', style='Heading 1')
doc.add_paragraph('Quote', style='Quote')
doc.add_paragraph('List item', style='List Bullet')

# Common styles:
# - 'Normal', 'Heading 1-9', 'Title', 'Subtitle'
# - 'Quote', 'Intense Quote', 'List Bullet', 'List Number'
# - 'Table Grid', 'Light Shading', 'Medium Grid 1'

Best Practices

  1. Structure First: Plan document hierarchy before coding
  2. Use Styles: Consistent formatting via styles, not manual formatting
  3. Save Often: Call doc.save() periodically for large documents
  4. Handle Errors: Check file existence before opening
  5. Clean Up: Remove template placeholders after filling

Common Patterns

Report Template

def create_report(title, sections):
    doc = Document()
    doc.add_heading(title, 0)
    doc.add_paragraph(f'Generated: {datetime.now()}')
    
    for section_title, content in sections.items():
        doc.add_heading(section_title, 1)
        doc.add_paragraph(content)
    
    return doc

Table from Data

def add_data_table(doc, headers, rows):
    table = doc.add_table(rows=1, cols=len(headers))
    table.style = 'Table Grid'
    
    # Headers
    for i, header in enumerate(headers):
        table.rows[0].cells[i].text = header
        table.rows[0].cells[i].paragraphs[0].runs[0].bold = True
    
    # Data rows
    for row_data in rows:
        row = table.add_row()
        for i, value in enumerate(row_data):
            row.cells[i].text = str(value)
    
    return table

Mail Merge Pattern

def fill_template(template_path, replacements):
    doc = Document(template_path)
    
    for para in doc.paragraphs:
        for key, value in replacements.items():
            if f'{{{key}}}' in para.text:
                para.text = para.text.replace(f'{{{key}}}', value)
    
    return doc

Examples

Example 1: Create a Business Letter

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

doc = Document()

# Letterhead
doc.add_paragraph('ACME Corporation')
doc.add_paragraph('123 Business Ave, Suite 100')
doc.add_paragraph('New York, NY 10001')
doc.add_paragraph()

# Date
doc.add_paragraph(datetime.now().strftime('%B %d, %Y'))
doc.add_paragraph()

# Recipient
doc.add_paragraph('Mr. John Smith')
doc.add_paragraph('XYZ Company')
doc.add_paragraph('456 Industry Blvd')
doc.add_paragraph('Chicago, IL 60601')
doc.add_paragraph()

# Salutation
doc.add_paragraph('Dear Mr. Smith,')
doc.add_paragraph()

# Body
body = """We are pleased to inform you that your proposal has been accepted...

[Letter body continues...]

Thank you for your continued partnership."""

for para_text in body.split('

'):
    doc.add_paragraph(para_text)

doc.add_paragraph()
doc.add_paragraph('Sincerely,')
doc.add_paragraph()
doc.add_paragraph()
doc.add_paragraph('Jane Doe')
doc.add_paragraph('CEO, ACME Corporation')

doc.save('business_letter.docx')

Example 2: Create a Report with Table

from docx import Document
from docx.shared import Inches

doc = Document()
doc.add_heading('Q4 Sales Report', 0)

# Executive Summary
doc.add_heading('Executive Summary', 1)
doc.add_paragraph('Q4 2024 showed strong growth across all regions...')

# Sales Table
doc.add_heading('Regional Performance', 1)

table = doc.add_table(rows=1, cols=4)
table.style = 'Medium Grid 1 Accent 1'

headers = ['Region', 'Q3 Sales', 'Q4 Sales', 'Growth']
for i, header in enumerate(headers):
    table.rows[0].cells[i].text = header

data = [
    ['North America', '$1.2M', '$1.5M', '+25%'],
    ['Europe', '$800K', '$950K', '+18%'],
    ['Asia Pacific', '$600K', '$750K', '+25%'],
]

for row_data in data:
    row = table.add_row()
    for i, value in enumerate(row_data):
        row.cells[i].text = value

doc.save('sales_report.docx')

Limitations

  • Cannot execute macros or VBA code
  • Complex templates may lose some formatting
  • Limited support for advanced features (SmartArt, Charts)
  • No direct PDF conversion (use separate tool)
  • Track changes reading is limited

Installation

pip install python-docx

Resources

  • python-docx Documentation
  • GitHub Repository
  • Office Open XML Spec