How to Connect Shapes with Connectors in Python

How to Connect Shapes with Connectors in Python

This guide shows how to connect shapes with connectors in PowerPoint slides using Aspose.Slides FOSS for Python. Call slide.shapes.add_connector() to create a connector, then set conn.start_shape_connected_to and conn.end_shape_connected_to to link two shapes. The most common connector type is BENT_CONNECTOR3, which routes around obstacles with a single elbow bend.


Prerequisites

Install the library in your Python environment by running the following pip command before running any examples:

pip install aspose-slides-foss

Connection Site Indexes

Every shape has four numbered connection sites:

IndexPosition
0Top center
1Left center
2Bottom center
3Right center

Connect Two Shapes

Create the two shapes, add a connector with slide.shapes.add_connector(), then set start_shape_connected_to and end_shape_connected_to to link them:

from aspose.slides_foss import ShapeType
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    # Add two rectangles
    box1 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 200, 200, 100)
    box2 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 450, 200, 200, 100)

    box1.add_text_frame("Start")
    box2.add_text_frame("End")

    # Add a bent connector (initial bounds are overwritten by the connection)
    conn = slide.shapes.add_connector(ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10)

    # Connect right side of box1 (site 3) to left side of box2 (site 1)
    conn.start_shape_connected_to = box1
    conn.start_shape_connection_site_index = 3
    conn.end_shape_connected_to = box2
    conn.end_shape_connection_site_index = 1

    prs.save("connected.pptx", SaveFormat.PPTX)

The placeholder bounds (0, 0, 10, 10) passed to add_connector are ignored once the connection endpoints are set; PowerPoint re-routes the connector to the attached shapes.


Connector Types

Use ShapeType constants to select the connector routing style. Common options include straight lines, single-elbow, and double-elbow connectors:

from aspose.slides_foss import ShapeType

# Straight line
ShapeType.STRAIGHT_CONNECTOR1

# Single elbow (L-shape)
ShapeType.BENT_CONNECTOR2

# Double elbow (Z-shape): most common
ShapeType.BENT_CONNECTOR3

# Curved connector
ShapeType.CURVED_CONNECTOR3

Style the Connector Line

Set the connector’s line color, width, and dash style via conn.line_format after creating the connector:

from aspose.slides_foss import ShapeType, LineDashStyle
from aspose.slides_foss.drawing import Color
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    box1 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 50, 150, 180, 80)
    box2 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 500, 300, 180, 80)

    conn = slide.shapes.add_connector(ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10)
    conn.start_shape_connected_to = box1
    conn.start_shape_connection_site_index = 2   # bottom of box1
    conn.end_shape_connected_to = box2
    conn.end_shape_connection_site_index = 0     # top of box2

    # Style: dashed blue line, 2 pt width
    lf = conn.line_format
    lf.width = 2.0
    lf.fill_format.solid_fill_color.color = Color.from_argb(255, 0, 0, 255)  # blue
    lf.dash_style = LineDashStyle.DASH

    prs.save("styled-connector.pptx", SaveFormat.PPTX)

Flowchart with Multiple Connectors

Build a multi-step flowchart by creating several shapes and adding a BENT_CONNECTOR3 connector between each consecutive pair:

from aspose.slides_foss import ShapeType
import aspose.slides_foss as slides
from aspose.slides_foss.export import SaveFormat

with slides.Presentation() as prs:
    slide = prs.slides[0]

    # Three-step flowchart
    step1 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 350, 50, 200, 70)
    step2 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 350, 220, 200, 70)
    step3 = slide.shapes.add_auto_shape(ShapeType.RECTANGLE, 350, 390, 200, 70)

    step1.add_text_frame("Step 1")
    step2.add_text_frame("Step 2")
    step3.add_text_frame("Step 3")

    def connect_vertical(shapes, top_shape, bottom_shape):
        conn = shapes.add_connector(ShapeType.BENT_CONNECTOR3, 0, 0, 10, 10)
        conn.start_shape_connected_to = top_shape
        conn.start_shape_connection_site_index = 2   # bottom
        conn.end_shape_connected_to = bottom_shape
        conn.end_shape_connection_site_index = 0     # top
        return conn

    connect_vertical(slide.shapes, step1, step2)
    connect_vertical(slide.shapes, step2, step3)

    prs.save("flowchart.pptx", SaveFormat.PPTX)

Read Connector Properties

Load an existing presentation and inspect each connector’s start_shape_connected_to and end_shape_connected_to by casting shapes to Connector:

from aspose.slides_foss import Connector
import aspose.slides_foss as slides

with slides.Presentation("connected.pptx") as prs:
    for shape in prs.slides[0].shapes:
        if isinstance(shape, Connector):
            start = shape.start_shape_connected_to
            end = shape.end_shape_connected_to
            print(f"Connector: {getattr(start, 'name', '?')}{getattr(end, 'name', '?')}")

See Also

 English