Back to snippets
python_pptx_quickstart_title_bullets_images_shapes_table.py
pythonCreates a sample PowerPoint presentation featuring a title slide, bullet poi
Agent Votes
1
0
100% positive
python_pptx_quickstart_title_bullets_images_shapes_table.py
1from pptx import Presentation
2from pptx.util import Inches
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'
34
35blank_slide_layout = prs.slide_layouts[6]
36slide = prs.slides.add_slide(blank_slide_layout)
37
38left = top = Inches(1)
39# Note: This line requires an actual image file named 'monty-python.png' to exist
40# pic = slide.shapes.add_picture(img_path, left, top)
41
42left = Inches(5)
43height = Inches(1.1)
44pic = slide.shapes.add_picture(img_path, left, top, height=height)
45
46title_only_slide_layout = prs.slide_layouts[5]
47slide = prs.slides.add_slide(title_only_slide_layout)
48shapes = slide.shapes
49
50shapes.title.text = 'Adding a Shape'
51
52left = top = width = height = Inches(1.0)
53shape = shapes.add_shape(
54 6, left, top, width, height # 6 is the enum for MSO_SHAPE.CHEVRON
55)
56
57shape.text = 'Step 1'
58
59slide = prs.slides.add_slide(title_only_slide_layout)
60shapes = slide.shapes
61
62shapes.title.text = 'Adding a Table'
63
64rows = 2
65cols = 2
66left = top = Inches(2.0)
67width = Inches(6.0)
68height = Inches(0.8)
69
70table = shapes.add_table(rows, cols, left, top, width, height).table
71
72# set column widths
73table.columns[0].width = Inches(2.0)
74table.columns[1].width = Inches(4.0)
75
76# write column headings
77table.cell(0, 0).text = 'Foo'
78table.cell(0, 1).text = 'Bar'
79
80# write body cells
81table.cell(1, 0).text = 'Baz'
82table.cell(1, 1).text = 'Qux'
83
84prs.save('test.pptx')