Docx Manipulation

作者 claude-office-skills9c4c7d5cd281MIT499 个星标收录于 2026年10月8日更新于 2026年10月8日仓库8个月前更新

Create, edit, and manipulate Word documents programmatically using python-docx

仅含说明Documents & Office
AI 生成的概览

使用 python-docx 库以编程方式创建、编辑和操作 Word .docx 文档。

功能
该技能指导智能体生成 python-docx 代码,用于构建和修改 Microsoft Word 文档。内容涵盖文档创建、标题、段落、表格、图片、样式、页面设置、页眉页脚以及模板占位符填充。产出为 .docx 文件,例如报告、信函和合同。
适用场景
适用于需要以编程方式生成或编辑 Word 文档的场景,例如根据内容或数据生成格式化的报告、商务信函或合同。也适合填充模板占位符,以及合并 .docx 文件或从中提取文本。
运行要求
需要 python-docx 包和 Python 运行时来执行生成的代码。该技能本身不附带脚本,仅为说明文档。部分列出的能力引用了 office-mcp 服务器及其工具,如 create_docx 和 merge_docx_files。

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

python
from docx import Documentfrom docx.shared import Inches, Pt, Cmfrom docx.enum.text import WD_ALIGN_PARAGRAPHfrom docx.enum.style import WD_STYLE_TYPE
# Create new documentdoc = Document()
# Or open existingdoc = 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
python
# Add heading (level 0-9)doc.add_heading('Main Title', level=0)doc.add_heading('Section Title', level=1)
# Add paragraphpara = doc.add_paragraph('Normal text here')
# Add styled paragraphdoc.add_paragraph('Note: Important!', style='Intense Quote')
# Add with inline formattingpara = doc.add_paragraph()para.add_run('Bold text').bold = Truepara.add_run(' and ')para.add_run('italic text').italic = True
Tables
python
# Create tabletable = doc.add_table(rows=3, cols=3)table.style = 'Table Grid'
# Add contenttable.cell(0, 0).text = 'Header 1'table.rows[0].cells[1].text = 'Header 2'
# Add row dynamicallyrow = table.add_row()row.cells[0].text = 'New data'
# Merge cellsa = table.cell(0, 0)b = table.cell(0, 2)a.merge(b)
Images
python
# Add image with sizedoc.add_picture('image.png', width=Inches(4))
# Add to specific paragraphpara = doc.add_paragraph()run = para.add_run()run.add_picture('logo.png', width=Inches(1.5))

Formatting

Paragraph Formatting
python
from docx.enum.text import WD_ALIGN_PARAGRAPHfrom docx.shared import Pt, Inches
para = doc.add_paragraph('Formatted text')para.alignment = WD_ALIGN_PARAGRAPH.CENTERpara.paragraph_format.line_spacing = 1.5para.paragraph_format.space_after = Pt(12)para.paragraph_format.first_line_indent = Inches(0.5)
Character Formatting
python
run = para.add_run('Styled text')run.bold = Truerun.italic = Truerun.underline = Truerun.font.name = 'Arial'run.font.size = Pt(14)run.font.color.rgb = RGBColor(0x00, 0x00, 0xFF)  # Blue
Page Setup
python
from docx.enum.section import WD_ORIENTfrom docx.shared import Inches
section = doc.sections[0]section.page_width = Inches(11)section.page_height = Inches(8.5)section.orientation = WD_ORIENT.LANDSCAPEsection.left_margin = Inches(1)section.right_margin = Inches(1)

Headers & Footers

python
section = doc.sections[0]
# Headerheader = section.headerheader.paragraphs[0].text = "Company Name"header.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
# Footer with page numbersfooter = section.footerpara = footer.paragraphs[0]para.text = "Page "# Add page number fieldrun = para.add_run()fldChar1 = OxmlElement('w:fldChar')fldChar1.set(qn('w:fldCharType'), 'begin')run._r.append(fldChar1)# ... (field code for page number)

Styles

python
# Use built-in stylesdoc.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

python
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

python
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

python
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

python
from docx import Documentfrom docx.shared import Inches, Ptfrom docx.enum.text import WD_ALIGN_PARAGRAPHfrom datetime import datetime
doc = Document()
# Letterheaddoc.add_paragraph('ACME Corporation')doc.add_paragraph('123 Business Ave, Suite 100')doc.add_paragraph('New York, NY 10001')doc.add_paragraph()
# Datedoc.add_paragraph(datetime.now().strftime('%B %d, %Y'))doc.add_paragraph()
# Recipientdoc.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()
# Salutationdoc.add_paragraph('Dear Mr. Smith,')doc.add_paragraph()
# Bodybody = """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('\n\n'):    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

python
from docx import Documentfrom docx.shared import Inches
doc = Document()doc.add_heading('Q4 Sales Report', 0)
# Executive Summarydoc.add_heading('Executive Summary', 1)doc.add_paragraph('Q4 2024 showed strong growth across all regions...')
# Sales Tabledoc.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

bash
pip install python-docx

Resources

来源与署名

来源:claude-office-skills/skills位于docx-manipulation提交9c4c7d5

许可证: MIT

内容归原作者所有。SourceWeft 从公开仓库中收录这些内容。

举报或申请下架