title: Python office automation: Word and PowerPoint processing description: Learn how to use Python to automatically generate standardized Word documents and PPT presentations. Focus on mastering template-based batch document filling technology to achieve intelligent transformation of human resources and administrative work.

In daily HR, administration and even operations scenarios, repetitive labor-intensive document/PPT output is indispensable:

  • HR students may have to send out employment offers to 100 new employees
  • The administration should make quarterly commendation cards with company logo, number, and name.
  • Operations need to generate N weekly report attachments with a unified format but different core data.

These tasks are not difficult, but mechanical proofreading, replacement, and typesetting are time-consuming, labor-intensive, and error-prone. In fact, it can be done with one click using Python’s “template + data mapping” mode. Today I will talk about how to use two libraries to get started quickly.


Core tool library

In the Python ecosystem, these two areas are almost "standard solutions":

  • Word Processing:python-docx(Microsoft.docxExclusive, old version.docNeed to be converted to a new format)
  • PPT Processing:python-pptx(Only supports.pptx, in the same way, convert the old version first)

Install it first:

# 建议用国内镜像源更快,比如清华源
pip install python-docx python-pptx -i https://pypi.tuna.tsinghua.edu.cn/simple

1. Word automation: from basics to mass production

1.1 Easy to get started: write a one-page presentation document from scratch

python-docxThe design logic is very intuitive, and the.docxIt is disassembled into Document → Paragraph → Run three-layer structure, each layer performs its own duties:

LevelFunction
DocumentContainer for the entire document (save / save / still save)
ParagraphContainer for ordinary paragraphs and titles
Run"Independent format blocks" within the same paragraph (bold / font size / font color can be different)

Popular understanding: Document is a book; Paragraph is a natural paragraph in the book; Run is a sentence in a paragraph, which can have different formats. For example, "This is bold text" means that a paragraph contains two Runs.

First write a piece of code to generate a basic demo page:

from docx import Document
from docx.shared import Cm, Pt

# 1. 初始化空文档
doc = Document()

# 2. 添加标题、段落
doc.add_heading('Python 办公自动化入门实战', 0)  # 0 级 = 文档主标题

p = doc.add_paragraph('Python 不止能写爬虫、做 AI,还是 ')
# 添加一个「加粗+14号字」的独立块
highlight_run = p.add_run('职场摸鱼效率翻倍')
highlight_run.bold = True
highlight_run.font.size = Pt(14)
# 如果需要改颜色,可以 highlight_run.font.color.rgb = RGBColor(0xFF, 0x00, 0x00)
p.add_run(' 的神器!')

# 3. 插入图片(指定宽度,高度按比例自动缩放)
try:
    doc.add_picture('company_logo.png', width=Cm(5))
except FileNotFoundError:
    print("⚠️  找不到 company_logo.png,跳过图片插入")

# 4. 插入带边框的表格
table = doc.add_table(rows=1, cols=3)
table.style = 'Table Grid'  # 带网格线的表格样式
# 填充表头
header_cells = table.rows[0].cells
header_cells[0].text = '编号'
header_cells[1].text = '员工姓名'
header_cells[2].text = '入职部门'

# 5. 保存文件
doc.save('python_docx_demo.docx')
print("✅  基础 Word 文档生成成功!")

Run result: You will get a Word document containing titles, formatted paragraphs, pictures and tables. The whole process does not require manual typesetting.


1.2 Advanced Core: Batch Template Generation (Key Point!)

Writing documents from scratch can only be used for general presentations. What can really free your hands is "Word template-based replacement"**. This mode is especially suitable for making standard parts such as entry offers, resignation certificates, and commendation cards.

Tips for making templates

First, manually create perfect templates in Word, such as entry offer and resignation certificate, and then use double curly brackets to mark the placeholders:{employee_name}{entry_date}{position}

⚠️ Pitfall reminder: Do not select part of the placeholder at once to change the color and make it bold in Word! ! ! Otherwise, the generated Run will be torn apart, and some placeholders may not be replaced during replacement. The correct approach is: When writing placeholders, first write them as plain text, and then change the style of the entire placeholder in the template after the replacement logic is adjusted.

Complete batch replacement script

from docx import Document

def fill_word_template(template_path, data_list, output_prefix):
    """
    批量填充 Word 模板并保存
    :param template_path: 模板文件路径
    :param data_list: 待填充的数据列表(每个元素是字典)
    :param output_prefix: 输出文件的前缀
    """
    for index, item in enumerate(data_list):
        doc = Document(template_path)

        # 1. 替换段落中的占位符
        for p in doc.paragraphs:
            for run in p.runs:
                full_text = run.text
                for key, value in item.items():
                    placeholder = f'{{{key}}}'
                    if placeholder in full_text:
                        run.text = run.text.replace(placeholder, str(value))

        # 2. 替换表格中的占位符
        for table in doc.tables:
            for row in table.rows:
                for cell in row.cells:
                    for p in cell.paragraphs:
                        for run in p.runs:
                            full_text = run.text
                            for key, value in item.items():
                                placeholder = f'{{{key}}}'
                                if placeholder in full_text:
                                    run.text = run.text.replace(placeholder, str(value))

        # 3. 生成文件名(用姓名+序号,或者纯序号)
        output_file = f'{output_prefix}_{item["employee_name"]}_{index+1}.docx'
        doc.save(output_file)
        print(f"✅  已生成 {output_file}")


# ------------------ 测试数据 ------------------
employee_data = [
    {
        "employee_name": "骆昊",
        "entry_date": "2026-04-01",
        "position": "高级架构师",
        "department": "技术中台",
        "salary": "35K"
    },
    {
        "employee_name": "王大锤",
        "entry_date": "2026-04-02",
        "position": "全栈开发工程师",
        "department": "电商业务线",
        "salary": "18K"
    }
]

# ------------------ 调用函数 ------------------
if __name__ == "__main__":
    fill_word_template(
        template_path="entry_offer_template.docx",
        data_list=employee_data,
        output_prefix="2026Q2_入职Offer"
    )

Core idea: Traverse every Run in all paragraphs and tables in the document and check whether it contains{key}A placeholder in the form, which is replaced with the real data in the dictionary after it is found. Master this, and you can batch generate any number of customized documents in an instant.


2. PPT automation: use template to generate + content page

The core logic of PPT is Master Layout → Placeholder → Fill. Making or using a preset layout first is more stable and beautiful than writing a layout from scratch.

2.1 Basics: Use preset layout to write presentation PPT

python-pptxA set of layouts (index 0~9) is provided by default, for example:

  • 0: title slide
  • 1: title and content
  • 5: Blank page (commonly used for free play)

Let’s use these layouts to quickly generate a three-page PPT:

from pptx import Presentation
from pptx.util import Inches, Pt
from pptx.dml.color import RGBColor

# 1. 初始化演示文稿(默认宽屏 16:9)
prs = Presentation()

# 2. 第一页:标题页(布局索引 0)
title_layout = prs.slide_layouts[0]
slide1 = prs.slides.add_slide(title_layout)

title_shape = slide1.shapes.title
subtitle_shape = slide1.placeholders[1]   # 占位符索引 1 = 副标题
title_shape.text = "2026Q1 部门效率提升报告"
subtitle_shape.text = "汇报人:技术中台\n汇报时间:2026-03-25"

# 3. 第二页:标题 + 内容(布局索引 1)
content_layout = prs.slide_layouts[1]
slide2 = prs.slides.add_slide(content_layout)

slide2.shapes.title.text = "核心工作成果"

content_frame = slide2.placeholders[1].text_frame
content_frame.text = "1. 入职Offer/离职证明自动化上线"

p2 = content_frame.add_paragraph()
p2.text = "   - 效率提升约 300%"
p2.level = 1   # level 1 表示缩进一级

p3 = content_frame.add_paragraph()
p3.text = "   - 人工错误率降为0%"
p3.level = 1

# 4. 第三页:空白页 + 手动添加标题与图片(布局索引 5)
blank_layout = prs.slide_layouts[5]
slide3 = prs.slides.add_slide(blank_layout)

# 手动添加一个文本框作为标题
left = Inches(1)
top = Inches(0.5)
width = Inches(8)
height = Inches(1)
title_box = slide3.shapes.add_textbox(left, top, width, height)
title_box.text_frame.text = "自动化工具使用流程图"
title_box.text_frame.paragraphs[0].font.size = Pt(24)
title_box.text_frame.paragraphs[0].font.bold = True

# 插入图片
try:
    left_img = Inches(1)
    top_img = Inches(2)
    height_img = Inches(4)
    slide3.shapes.add_picture('automation_flowchart.png',
                              left_img, top_img, height=height_img)
except FileNotFoundError:
    print("⚠️  找不到流程图,跳过插入")

# 5. 保存
prs.save('2026Q1_效率提升报告.pptx')
print("✅  基础 PPT 生成成功!")

Note: The third page uses a blank layout and passesadd_textboxPlace the title yourself for a more flexible layout. In actual projects, you can also design the PPT master with placeholders in advance and fill it in a way similar to Word template replacement, which will achieve better results.


3. Summary and Outlook

1. Core points

  • Word
  • Keep in mind the three-tier structure:Document → Paragraph → Run
  • Focus on using templates to generate batches is the real tool for fishing.
  • Placeholders should be written in plain text, adjust and then change the style.
  • PPT
  • Prioritize using the default layout (index 0~9) and fill it with placeholders
  • For free typesetting needs, useadd_textboxadd_pictureWait for functions to be placed manually

2. Applicable scenarios

This type of script is a "Small once-and-for-all project". Although it may take 1 to 2 hours to debug placeholders and typesetting in the early stage, in the future, 10 to 100 documents/PPT can be uploaded every time it is executed. Not only does the efficiency soar, but also the formatting and typos are completely eliminated.

If you need more complex functions (such as inserting Excel charts into Word, inserting bar charts/line charts with data into PPT, linking with automatic email sending), you can continue to read the official documents of these two libraries, and the door to automation will open.