Basic example

This example demonstrates a variety of shape types, styling options, and layout techniques supported by pptx-shapes.

Basic example

Overview

The script:

  • Opens a base PowerPoint file with one empty slide (empty.pptx)

  • Adds shapes such as TextBox, Ellipse, Arrow, Arc, Rectangle, Polygon, and Group

  • Demonstrates: - Filled and outlined shapes - Text with styling and rotation - Groups of multiple elements - Use of coordinates, angles, and alignment - Color and opacity settings

Key features illustrated

  • Text and formatting: bold, size, color, alignment, rotation

  • Stroke styles: thickness, dashed lines, opacity

  • Fill styles: colors including hex, named, opacity

  • Shapes: rectangles (rounded or not), ellipses, arcs, arrows, polygons

  • Groups: reusable composite structures (e.g., labeled triangle, histogram)

  • Alignment and layout: both manual coordinates and automatic auto_fit

Example code

 1from pptx_shapes import Presentation
 2from pptx_shapes.enums import Align, ArrowType, LineDash, VerticalAlign
 3from pptx_shapes.shapes import Arc, Arrow, Ellipse, Group, Line, Polygon, Rectangle, TextBox
 4from pptx_shapes.style import ArrowHead, FillStyle, FontFormat, FontStyle, StrokeStyle
 5
 6
 7def main() -> None:
 8    with Presentation(presentation_path="empty.pptx") as presentation:
 9        presentation.add(shape=TextBox(
10            x=23, y=4, width=12, height=2, text="Hello from pptx-shapes!", angle=45, style=FontStyle(size=32), formatting=FontFormat(bold=True)
11        ))
12
13        presentation.add(shape=TextBox(
14            x=7.5, y=17.2, width=18.5, height=1.5,
15            text="Python library for adding basic geometric shapes directly to PowerPoint (.pptx) slides by editing the XML structure.",
16            style=FontStyle(size=16, align=Align.LEFT),
17            auto_fit=True
18        ))
19
20        # ellipses
21        presentation.add(shape=Ellipse(x=20, y=2, width=4, height=4, fill=FillStyle(color="#7699d4")))
22
23        # arrows
24        presentation.add(shape=Arrow(
25            x1=10, y1=9, x2=14, y2=11,
26            start_head=ArrowHead(head=ArrowType.OVAL),
27            end_head=ArrowHead(head=ArrowType.ARROW),
28            stroke=StrokeStyle(thickness=2)
29        ))
30
31        # arcs
32        presentation.add(shape=Arc(
33            x=24, y=9, width=5, height=8, start_angle=90, end_angle=270, angle=45, stroke=StrokeStyle(color="#f00", thickness=2.5, dash=LineDash.DASH_DOTTED)
34        ))
35        presentation.add(shape=Arc(
36            x=19.5, y=1.5, width=5, height=5, start_angle=5, end_angle=175, stroke=StrokeStyle(color="#7699d4", thickness=2, dash=LineDash.DOTTED)
37        ))
38        presentation.add(shape=Arc(
39            x=19.5, y=1.5, width=5, height=5, start_angle=185, end_angle=355, stroke=StrokeStyle(color="#7699d4", thickness=2, dash=LineDash.DASHED)
40        ))
41
42        # rectangles
43        presentation.add(shape=Rectangle(
44            x=18, y=8, width=4, height=8.5, radius=0.25, fill=FillStyle(color="#dd7373"), stroke=StrokeStyle(color="#222", thickness=3), angle=30
45        ))
46        presentation.add(shape=Rectangle(
47            x=27, y=14, width=3, height=3, radius=0, fill=FillStyle(color="#dd7373"), stroke=StrokeStyle(color="#222", thickness=1)
48        ))
49
50        # polygons
51        presentation.add(shape=Polygon(
52            points=[(11, 12), (13, 14), (11, 16), (9, 14), (11, 12)], fill=FillStyle(color="yellow"), stroke=StrokeStyle(color="magenta", thickness=2.5)
53        ))
54        presentation.add(shape=Polygon(
55            points=[(15, 5), (16, 6), (15, 7), (12, 7), (11, 6), (12, 5)], angle=45, fill=FillStyle(color="#88ff88")
56        ))
57
58        # groups
59        presentation.add(shape=Group(shapes=[
60            Line(x1=1, y1=1, x2=13, y2=1, stroke=StrokeStyle(thickness=2, color="#7699d4")),
61            Line(x1=1, y1=1, x2=1, y2=6, stroke=StrokeStyle(thickness=2, color="#dd7373")),
62            Line(x1=13, y1=1, x2=1, y2=6, stroke=StrokeStyle(thickness=2, color="#89dd73")),
63            TextBox(x=0.7, y=3.5, width=13, height=1, text="hypotenuse", angle=-22.6, style=FontStyle(size=18, color="#89dd73", vertical_align=VerticalAlign.TOP)),
64            TextBox(x=-2, y=3, width=5, height=1, text="kathete", angle=90, style=FontStyle(size=18, color="#dd7373", vertical_align=VerticalAlign.TOP)),
65            TextBox(x=1, y=0, width=12, height=1, text="kathete", style=FontStyle(size=18, color="#7699d4", vertical_align=VerticalAlign.BOTTOM))
66        ]))
67
68        presentation.add(shape=Group(shapes=[
69            Ellipse(x=4.5, y=6.0, width=2.0, height=3.5, fill=FillStyle(color="#dd7373", opacity=0.5), stroke=StrokeStyle(color="black", thickness=2, opacity=0.75)),
70            Ellipse(x=3.0, y=8.5, width=3.5, height=2.0, fill=FillStyle(color="#dd7373", opacity=0.25), stroke=StrokeStyle(color="black", opacity=0.25), angle=-45),
71            Ellipse(x=5.0, y=8.5, width=3.5, height=2.0, fill=FillStyle(color="#dd7373", opacity=0.85), stroke=StrokeStyle(color="black", opacity=0.85), angle=45)
72        ]))
73
74        presentation.add(shape=Group(shapes=[
75            TextBox(x=1, y=15, width=4.8, height=1, text="little histogram", style=FontStyle(size=20, color="#7699d4")),
76            Rectangle(x=1, y=16, width=1.2, height=2.7, radius=0.2, fill=FillStyle(color="#7699d4"), stroke=StrokeStyle(color="#fff")),
77            Rectangle(x=2.2, y=16.4, width=1.2, height=2.3, radius=0.2, fill=FillStyle(color="#7699d4"), stroke=StrokeStyle(color="#fff")),
78            Rectangle(x=3.4, y=17, width=1.2, height=1.7, radius=0.2, fill=FillStyle(color="#7699d4"), stroke=StrokeStyle(color="#fff")),
79            Rectangle(x=4.6, y=16.1, width=1.2, height=2.6, radius=0.2, fill=FillStyle(color="#7699d4"), stroke=StrokeStyle(color="#fff"))
80        ]))
81
82        presentation.save("basic.pptx")
83
84
85if __name__ == "__main__":
86    main()

Output

The script generates a file basic.pptx with all the illustrated shapes. Below is a preview of what it might contain:

  • A diagonal “Hello from pptx-shapes!” textbox

  • Ellipses with fills and stroke transparency

  • Colored and dashed arcs

  • Classic arrows with arrowheads

  • Rotated and grouped shapes with styled text

  • Custom polygons with fill and outline

  • A small histogram composed of styled rectangles

Note

This example assumes you have an empty.pptx file to use as a base presentation template.