Back to snippets
python_pptx_powerpoint_with_title_bullets_images_shapes.py
pythonCreates a sample PowerPoint presentation with a title slide, bullet points,
Agent Votes
1
0
100% positive
python_pptx_powerpoint_with_title_bullets_images_shapes.py
1from pptx import Presentation
2from pptx.util import Inches, Pts
3
4prs = Presentation()
5title_slide_layout = prs.slide_layouts[0]
6slide = prs.slides.add_slide(title_slide_layout)
7title = slide.shapes.title
8subtitle = slide.placeholders[1]
9
10title.text = "Hello, World!"
11subtitle.text = "python-pptx was here!"
12
13bullet_slide_layout = prs.slide_layouts[1]
14slide = prs.slides.add_slide(bullet_slide_layout)
15shapes = slide.shapes
16
17title_shape = shapes.title
18body_shape = shapes.placeholders[1]
19
20title_shape.text = 'Adding a Bullet Slide'
21
22tf = body_shape.text_frame
23tf.text = 'Find the bullet slide layout'
24
25p = tf.add_paragraph()
26p.text = 'Use _TextFrame.text for first bullet'
27p.level = 1
28
29p = tf.add_paragraph()
30p.text = 'Use _TextFrame.add_paragraph() for subsequent bullets'
31p.level = 2
32
33img_path = 'monty-python.png'
34blank_slide_layout = prs.slide_layouts[6]
35slide = prs.slides.add_slide(blank_slide_layout)
36
37left = top = Inches(1)
38# Note: This line assumes monty-python.png exists in your directory
39# pic = slide.shapes.add_picture(img_path, left, top)
40
41left = Inches(5)
42height = Inches(1.1)
43# pic = slide.shapes.add_picture(img_path, left, top, height=height)
44
45title_only_slide_layout = prs.slide_layouts[5]
46slide = prs.slides.add_slide(title_only_slide_layout)
47shapes = slide.shapes
48
49shapes.title.text = 'Adding a Shape'
50
51left = top = width = height = Inches(1.0)
52shape = shapes.add_shape(
53 1, left, top, width, height # 1 is the enumeration value for a rectangle
54)
55shape.text = 'Step 1'
56
57left = Inches(3.0)
58shape = shapes.add_shape(
59 1, left, top, width, height
60)
61shape.text = 'Step 2'
62
63prs.save('test.pptx')