目次
1. What is python-pptx?
python-pptx is a library that allows you to create, edit, and save PowerPoint presentations directly from Python programs. This eliminates the need for manual work and enables automated generation of dynamic presentation materials based on data, as well as streamlining repetitive tasks.1.1 Main Uses of python-pptx
- Automated Presentation Creation: Automatically generate slides for meetings and reports based on data.
- Efficient Editing: Add slides or text to existing PowerPoint files and update presentation content quickly.
1.2 Advantages of Using Python for PowerPoint
Compared to manual work in PowerPoint, using python-pptx allows for automation through programming. This makes it possible to generate a large number of slides at once or streamline recurring report creation, significantly reducing work time.2. Setting Up python-pptx
2.1 Installation Steps
Installing python-pptx is very simple. Just run the following command:pip install python-pptx
If you don’t have a Python environment set up, you can use conda
to create an environment and install the library.2.2 Basic Structure of python-pptx
Import the library and use thePresentation()
class to create a new presentation. This forms the basis of PowerPoint manipulation.from pptx import Presentation
prs = Presentation()
This code creates a new presentation, preparing it for adding slides and text.
3. Creating and Saving Presentations
3.1 Creating a New Presentation
First, let’s explain how to create and save a presentation using python-pptx. The following code creates and saves a new presentation file:from pptx import Presentation
prs = Presentation()
prs.save('new_presentation.pptx')
This saves a PowerPoint file in the current directory with the specified name.3.2 How to Add Slides
To add slides to a presentation, use theadd_slide()
method. You can choose from 11 layout templates.slide_layout = prs.slide_layouts[0] # Select Title Slide
slide = prs.slides.add_slide(slide_layout)
This code adds a new slide based on the specified layout.4. Manipulating Slide Content
4.1 Inserting and Formatting Text
When inserting text into a slide, use placeholders (for title or subtitle positions). Then, format the text with theTextFrame
object. Here’s how to insert and format text:title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Presentation Title"
subtitle.text = "Subtitle"
You can also change font size and color, or add line breaks:title.text = "Slide
Auto-generated with Python"
4.2 Adding Images and Shapes
To insert images into a slide, use theadd_picture()
method. You can also specify position and size.from pptx.util import Inches
img_path = 'image.png'
left = Inches(1)
top = Inches(2)
slide.shapes.add_picture(img_path, left, top)
Additionally, you can add shapes available in PowerPoint’s shape menu and customize their design:from pptx.enum.shapes import MSO_SHAPE
shape = slide.shapes.add_shape(MSO_SHAPE.ROUNDED_RECTANGLE, Inches(1), Inches(1), Inches(2), Inches(1))
shape.text = "Text in Shape"

5. Advanced Automation Techniques
5.1 Automatically Generating Slides
You can use loops to automatically generate a large number of slides. For example, when creating a presentation based on data, the following code is useful:for i in range(10):
slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(slide_layout)
title = slide.shapes.title
title.text = f"Slide {i+1}"
This code generates 10 slides automatically, each with a sequential number.5.2 Editing Existing Presentations
You can also load and edit existing PowerPoint files. For example, to update a title, use this code:prs = Presentation('existing_presentation.pptx')
slide = prs.slides[0]
title = slide.shapes.title
title.text = "Updated Title"
prs.save('updated_presentation.pptx')