docx-manipulation
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDOCX Manipulation Skill
DOCX文档处理技能
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.
本技能支持使用python-docx库以编程方式创建、编辑和处理Microsoft Word(.docx)文档。无需手动编辑,即可创建包含规范格式、样式、表格和图片的专业文档。
How to Use
使用方法
- Describe what you want to create or modify in a Word document
- Provide any source content (text, data, images)
- 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"
- 描述你想要在Word文档中创建或修改的内容
- 提供任何源内容(文本、数据、图片)
- 我将生成python-docx代码并执行
示例提示:
- "创建包含标题、副标题和表格的专业报告"
- "为该文档添加页眉和页脚"
- "生成带有占位符的合同文档"
- "将此Markdown内容转换为带样式的Word文档"
Domain Knowledge
领域知识
python-docx Fundamentals
python-docx基础
python
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_TYPEpython
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_TYPECreate new document
Create new document
doc = Document()
doc = Document()
Or open existing
Or open existing
doc = Document('existing.docx')
undefineddoc = Document('existing.docx')
undefinedDocument Structure
文档结构
Document
├── sections (margins, orientation, size)
├── paragraphs (text with formatting)
├── tables (rows, cells, merged cells)
├── pictures (inline images)
└── styles (predefined formatting)Document
├── sections (边距、方向、尺寸)
├── paragraphs (带格式的文本)
├── tables (行、单元格、合并单元格)
├── pictures (嵌入式图片)
└── styles (预定义格式)Adding Content
添加内容
Paragraphs & Headings
段落与标题
python
undefinedpython
undefinedAdd heading (level 0-9)
Add heading (level 0-9)
doc.add_heading('Main Title', level=0)
doc.add_heading('Section Title', level=1)
doc.add_heading('Main Title', level=0)
doc.add_heading('Section Title', level=1)
Add paragraph
Add paragraph
para = doc.add_paragraph('Normal text here')
para = doc.add_paragraph('Normal text here')
Add styled paragraph
Add styled paragraph
doc.add_paragraph('Note: Important!', style='Intense Quote')
doc.add_paragraph('Note: Important!', style='Intense Quote')
Add with inline formatting
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
undefinedpara = doc.add_paragraph()
para.add_run('Bold text').bold = True
para.add_run(' and ')
para.add_run('italic text').italic = True
undefinedTables
表格
python
undefinedpython
undefinedCreate table
Create table
table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'
table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'
Add content
Add content
table.cell(0, 0).text = 'Header 1'
table.rows[0].cells[1].text = 'Header 2'
table.cell(0, 0).text = 'Header 1'
table.rows[0].cells[1].text = 'Header 2'
Add row dynamically
Add row dynamically
row = table.add_row()
row.cells[0].text = 'New data'
row = table.add_row()
row.cells[0].text = 'New data'
Merge cells
Merge cells
a = table.cell(0, 0)
b = table.cell(0, 2)
a.merge(b)
undefineda = table.cell(0, 0)
b = table.cell(0, 2)
a.merge(b)
undefinedImages
图片
python
undefinedpython
undefinedAdd image with size
Add image with size
doc.add_picture('image.png', width=Inches(4))
doc.add_picture('image.png', width=Inches(4))
Add to specific paragraph
Add to specific paragraph
para = doc.add_paragraph()
run = para.add_run()
run.add_picture('logo.png', width=Inches(1.5))
undefinedpara = doc.add_paragraph()
run = para.add_run()
run.add_picture('logo.png', width=Inches(1.5))
undefinedFormatting
格式设置
Paragraph Formatting
段落格式
python
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)python
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
字符格式
python
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) # Bluepython
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) # BluePage Setup
页面设置
python
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)python
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
页眉与页脚
python
section = doc.sections[0]python
section = doc.sections[0]Header
Header
header = section.header
header.paragraphs[0].text = "Company Name"
header.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
header = section.header
header.paragraphs[0].text = "Company Name"
header.paragraphs[0].alignment = WD_ALIGN_PARAGRAPH.CENTER
Footer with page numbers
Footer with page numbers
footer = section.footer
para = footer.paragraphs[0]
para.text = "Page "
footer = section.footer
para = footer.paragraphs[0]
para.text = "Page "
Add page number field
Add page number field
run = para.add_run()
fldChar1 = OxmlElement('w:fldChar')
fldChar1.set(qn('w:fldCharType'), 'begin')
run._r.append(fldChar1)
run = para.add_run()
fldChar1 = OxmlElement('w:fldChar')
fldChar1.set(qn('w:fldCharType'), 'begin')
run._r.append(fldChar1)
... (field code for page number)
... (field code for page number)
undefinedundefinedStyles
样式
python
undefinedpython
undefinedUse built-in 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')
doc.add_paragraph('Heading', style='Heading 1')
doc.add_paragraph('Quote', style='Quote')
doc.add_paragraph('List item', style='List Bullet')
Common styles:
Common styles:
- 'Normal', 'Heading 1-9', 'Title', 'Subtitle'
- 'Normal', 'Heading 1-9', 'Title', 'Subtitle'
- 'Quote', 'Intense Quote', 'List Bullet', 'List Number'
- 'Quote', 'Intense Quote', 'List Bullet', 'List Number'
- 'Table Grid', 'Light Shading', 'Medium Grid 1'
- 'Table Grid', 'Light Shading', 'Medium Grid 1'
undefinedundefinedBest Practices
最佳实践
- Structure First: Plan document hierarchy before coding
- Use Styles: Consistent formatting via styles, not manual formatting
- Save Often: Call periodically for large documents
doc.save() - Handle Errors: Check file existence before opening
- Clean Up: Remove template placeholders after filling
- 先搭结构:编码前规划文档层级
- 使用样式:通过样式实现统一格式,而非手动设置
- 经常保存:处理大型文档时定期调用
doc.save() - 错误处理:打开文件前检查文件是否存在
- 清理内容:填充完成后移除模板占位符
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 docpython
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 docTable 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 tablepython
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 tableMail 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 docpython
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 docExamples
示例
Example 1: Create a Business Letter
示例1:创建商务信函
python
from docx import Document
from docx.shared import Inches, Pt
from docx.enum.text import WD_ALIGN_PARAGRAPH
from datetime import datetime
doc = Document()python
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
Letterhead
doc.add_paragraph('ACME Corporation')
doc.add_paragraph('123 Business Ave, Suite 100')
doc.add_paragraph('New York, NY 10001')
doc.add_paragraph()
doc.add_paragraph('ACME Corporation')
doc.add_paragraph('123 Business Ave, Suite 100')
doc.add_paragraph('New York, NY 10001')
doc.add_paragraph()
Date
Date
doc.add_paragraph(datetime.now().strftime('%B %d, %Y'))
doc.add_paragraph()
doc.add_paragraph(datetime.now().strftime('%B %d, %Y'))
doc.add_paragraph()
Recipient
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()
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
Salutation
doc.add_paragraph('Dear Mr. Smith,')
doc.add_paragraph()
doc.add_paragraph('Dear Mr. Smith,')
doc.add_paragraph()
Body
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('\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')
undefinedbody = """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')
undefinedExample 2: Create a Report with Table
示例2:创建带表格的报告
python
from docx import Document
from docx.shared import Inches
doc = Document()
doc.add_heading('Q4 Sales Report', 0)python
from docx import Document
from docx.shared import Inches
doc = Document()
doc.add_heading('Q4 Sales Report', 0)Executive Summary
Executive Summary
doc.add_heading('Executive Summary', 1)
doc.add_paragraph('Q4 2024 showed strong growth across all regions...')
doc.add_heading('Executive Summary', 1)
doc.add_paragraph('Q4 2024 showed strong growth across all regions...')
Sales Table
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')
undefineddoc.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')
undefinedLimitations
局限性
- 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
- 无法执行宏或VBA代码
- 复杂模板可能会丢失部分格式
- 对高级功能(SmartArt、图表)的支持有限
- 不支持直接PDF转换(需使用单独工具)
- 修订记录的读取功能有限
Installation
安装
bash
pip install python-docxbash
pip install python-docx