我正在尝试在形状之间添加连接器,但我遇到了它们的对齐和大小问题。
我有一个占位符形状和几个末端形状。每个
end_shape
都是地图上一个国家的形状。国家/地区形状是称为“地图”的组形状的一部分。:
我希望将连接器的端点放置在其国家/地区的中心。 连接器从表格占位符的边缘开始到国家/地区 end_shape
的
center。
我的问题是整个连接器显得太大,并且其端点未与国家/地区中心正确对齐。 代码:
def add_connector(slide, end_shape, placeholder_idx):
placeholder = slide.placeholders[placeholder_idx]
end_center_x = end_shape.left + (end_shape.width // 2)
end_center_y = end_shape.top + (end_shape.height // 2)
connector = slide.shapes.add_connector(
MSO_CONNECTOR.ELBOW,
placeholder.left,
placeholder.top,
end_center_x,
end_center_y,
)
connector.begin_connect(placeholder, 3)
line_elem = connector.line._get_or_add_ln()
line_elem.append(
parse_xml(
'<a:tailEnd type="oval" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>'
)
)
connector.line.width = Pt(0.5)
connector.line.color.rgb = RGBColor(32, 64, 140)
return connector
def find_country_shape(slide, country_name):
map_shape = next(shape for shape in slide.shapes if shape.name == "Map")
return next(
(
shape
for shape in map_shape.shapes
if shape.name.lower() == country_name.lower()
),
None,
)
现在我有一个临时计算解决方案,但端点偏离中心并且根本不理想,特别是如果我要增加表格占位符的大小..
def add_connector(slide, end_shape, placeholder_idx):
placeholder = slide.placeholders[placeholder_idx]
scale_factor = 0.63
end_center_x = int(
placeholder.left
+ scale_factor * (end_shape.left + (end_shape.width // 2) - placeholder.left)
)
end_center_y = int(
placeholder.top
+ scale_factor * (end_shape.top + (end_shape.height // 2) - placeholder.top)
)
我的问题是,在我的第一个代码中,我计算了该国家/地区的中心点并将其设置为连接器的端点,但它显示这个巨大的连接器并不靠近该国家/地区。
问题在于 PowerPoint 中如何使用连接器的对齐方式、尺寸和缩放系数。
您可以尝试以下代码。
from pptx.util import Pt
from pptx.dml.color import RGBColor
from pptx.enum.shapes import MSO_CONNECTOR
from pptx.oxml import parse_xml
def add_connector(slide, end_shape, placeholder_idx):
placeholder = slide.placeholders[placeholder_idx]
# Calculate the center of the placeholder
placeholder_center_x = placeholder.left + (placeholder.width // 2)
placeholder_center_y = placeholder.top + (placeholder.height // 2)
# Calculate the center of the end shape
end_center_x = end_shape.left + (end_shape.width // 2)
end_center_y = end_shape.top + (end_shape.height // 2)
# Add the connector
connector = slide.shapes.add_connector(
MSO_CONNECTOR.ELBOW,
placeholder_center_x,
placeholder_center_y,
end_center_x,
end_center_y,
)
# Connect the connector to the shapes
connector.begin_connect(placeholder, 2) # 2 is the middle of the placeholder
connector.end_connect(end_shape, 2) # 2 is the middle of the end shape
# Customize the connector's appearance
line_elem = connector.line._get_or_add_ln()
line_elem.append(
parse_xml(
'<a:tailEnd type="oval" xmlns:a="http://schemas.openxmlformats.org/drawingml/2006/main"/>'
)
)
connector.line.width = Pt(0.5)
connector.line.color.rgb = RGBColor(32, 64, 140)
return connector
def find_country_shape(slide, country_name):
map_shape = next(shape for shape in slide.shapes if shape.name == "Map")
return next(
(
shape
for shape in map_shape.shapes
if shape.name.lower() == country_name.lower()
),
None,
)
说明:
计算中心:精确计算占位符和末端形状的中心。
添加连接器: 使用计算的中心点添加连接器。
连接形状:
begin_connect
和end_connect
方法用于将连接器连接到占位符和末端形状的中间。
自定义外观: 连接器的外观与以前一样自定义。 这应确保连接器正确对齐和调整大小,其端点位于国家/地区形状的中心。