如何在 Python 中使用连接线连接形状

如何在 Python 中使用连接线连接形状

在 Aspose.Slides FOSS 中,连接线是附着在其他形状的 连接点 上的线形状。当您移动已连接的形状时,连接线的端点会随之移动。最常用的连接线类型是 BENT_CONNECTOR3,它在遇到障碍物时会以单个拐角弯的方式绕行。


先决条件

pip install aspose-slides-foss

连接站点索引

每个形状都有四个编号的连接点:

IndexPosition
0顶部居中
1左侧居中
2底部居中
3右侧居中

连接两个形状

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)

一旦设置了连接端点,传递给 add_connector 的占位符边界 (0, 0, 10, 10) 将被忽略;PowerPoint 会将连接线重新路由到附加的形状。


连接器类型

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

为连接线设定样式

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.blue
    lf.dash_style = LineDashStyle.DASH

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

具有多个连接器的流程图

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)

读取连接器属性

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', '?')}")

另请参阅

 中文