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:
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:
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:
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
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 slide1: title and content5: Blank page (commonly used for free play)
Let’s use these layouts to quickly generate a three-page 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, use
add_textbox、add_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.

