用 Codex 或 Claude 帮你安装 复制这段 Prompt,粘贴到 Codex、Claude 或其他助手里,让它检查 Skill 页面并帮你完成安装。
直接命令不会经过审查 Prompt;运行前请先检查来源。
npx skills add https://github.com/cxcscmu/SkillLearnBench --skill python-docx命令会保持在同一行。复制前请横向滚动并检查完整内容。
想先保存到本地?可下载 SkillsMP 当前能够提供的文件。
正在显示 SKILL.md
基于 SOC 职业分类
| name | python-docx |
| description | Programmatically read, modify, and create Word documents (.docx) with python-docx library |
The python-docx library allows you to create, read, and update Microsoft Word (.docx) files programmatically. It's essential for automating document generation, template filling, and Word document manipulation in Python.
pip install python-docx
from docx import Document
# Open existing document
doc = Document('template.docx')
# Create new document
doc = Document()
# Save document
doc.save('output.docx')
# Access all paragraphs
for para in doc.paragraphs:
print(para.text)
# Add new paragraph
new_para = doc.add_paragraph('Text here')
# Add text with formatting
run = new_para.add_run('Bold text')
run.bold = True
# Search through all paragraphs and runs
for para in doc.paragraphs:
for run in para.runs:
if '{{PLACEHOLDER}}' in run.text:
run.text = run.text.replace('{{PLACEHOLDER}}', 'replacement')
# Note: Text can be split across multiple runs!
# A safer approach:
full_text = ''.join(run.text for run in para.runs)
if '{{PLACEHOLDER}}' in full_text:
# Clear all runs and recreate with replacement
for run in para.runs:
run.text = ''
para.text = full_text.replace('{{PLACEHOLDER}}', 'replacement')
# Access table rows and cells
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
print(cell.text)
# Replace text in cell
cell.text = cell.text.replace('{{PLACEHOLDER}}', 'value')
The trickiest part of python-docx is that text in a paragraph can be split across multiple "runs" (especially in templates). For placeholder replacement:
def replace_text_in_paragraph(para, old_text, new_text):
"""Replace text handling split runs"""
if old_text in para.text:
# Clear existing runs
for run in para.runs:
run.text = ''
# Add new text as single run
para.text = new_text
Text Fragmentation: When you open a .docx file (especially templates), the same logical text might be split across multiple run objects due to formatting. Always check the full paragraph text before manipulating individual runs.
Preserve Formatting: When replacing text, consider whether you want to preserve the original formatting or apply new formatting.
Iterating Safely: If modifying document structure while iterating, make copies of lists first:
paragraphs = list(doc.paragraphs) # Create copy
for para in paragraphs:
# Safe to modify now
Headers/Footers: Accessed via doc.sections[0].header and doc.sections[0].footer
from docx import Document
def fill_template(template_path, replacements, output_path):
doc = Document(template_path)
# Replace in paragraphs
for para in doc.paragraphs:
for key, value in replacements.items():
if key in para.text:
para.text = para.text.replace(key, value)
# Replace in tables
for table in doc.tables:
for row in table.rows:
for cell in row.cells:
for key, value in replacements.items():
if key in cell.text:
cell.text = cell.text.replace(key, value)
doc.save(output_path)
para.text (full paragraph) to see actual content vs individual run.textlen(para.runs) to see if text is fragmenteddoc.element.xml to inspect underlying XML structure if needed