I'm trying to draw a line from shape's connecting point to another shape's connecting point using python-pptx. Below is the python source code. When I read the file with PowerPoint and move the shape, the line does not follow. The image is where I read in PowerPoint and the lines do not follow when I move the shape. How can I draw a line between connection points? Would anyone please help me to fix this code?
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 9 08:22:36 2020
@author: owner
"""
from pptx import Presentation
from pptx.util import Mm
from pptx.enum.shapes import MSO_SHAPE
from pptx.enum.shapes import MSO_CONNECTOR
prs = Presentation()
# A4 size
prs.slide_width = Mm(297)
prs.slide_height = Mm(210)
# Slide layout
SLD_LAYOUT_TITLE_AND_CONTENT = 6
slide_layout = prs.slide_layouts[SLD_LAYOUT_TITLE_AND_CONTENT]
# Get slide
sld = prs.slides.add_slide(slide_layout)
# Shape1
x1 = Mm(30)
y1 = Mm(50)
w1 = Mm(70)
h1 = Mm(50)
# Shape2
x2 = Mm(200)
y2 = Mm(50)
w2 = Mm(70)
h2 = Mm(50)
# Get autoshape object
shps = sld.shapes
# Shape1 rectangle
shp1 = shps.add_shape(
MSO_SHAPE.RECTANGLE, x1, y1, w1, h1
)
# Shape2 rectangle
shp2 = shps.add_shape(
MSO_SHAPE.RECTANGLE, x2, y2, w2, h2
)
# Connector co-ordinate(EMU)
# Shape1
cx1 = shp1.left + shp1.width
cy1 = shp1.top + int(shp1.height/2)
# SHape2
cx2 = shp2.left
cy2 = shp2.top + int(shp2.height/2)
# Draw line from (cx1, cy2) to (cx2, cy2)
line = shps.add_connector(
MSO_CONNECTOR.STRAIGHT, cx1, cy1, cx2, cy2
)
# File save
prs.save('connector_line.pptx')
Anchor connector end-points to shape connection points by using the Connector
methods described in the documentation here:
https://python-pptx.readthedocs.io/en/latest/api/shapes.html#connector-objects
Something like:
line.begin_connect(shp1, 0)
line.end_connect(shp2, 2)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.