我正在尝试使用 python-pptx 创建 PowerPoint 演示文稿,目前我正在尝试在 for 循环中创建形状和连接器。尽管连接器已创建并且确实连接到形状,但当我打开演示文稿本身时,它看起来不自然,有点像它被旋转了。然而,一旦我移动形状的顶部,它就开始看起来像预期的那样。
我尝试阅读文档(https://python-pptx.readthedocs.io/en/latest/api/shapes.html#connector-objects),但我没有看到任何改变连接器方式的选项“看起来”。
部分代码:
top_shape = shapes.add_shape(MSO_SHAPE.RECTANGLE, Inches(2), Inches(1), width, height)
for i in range(8):
bottom_shape = shapes.add_shape(MSO_SHAPE.RECTANGLE, left, top, width, height)
connector = shapes.add_connector(MSO_CONNECTOR.ELBOW, Cm(1), Cm(1), Cm(1), Cm(1))
connector.begin_connect(top_shape, 2)
connector.end_connect(bottom_shape, 0)
prs.save('test.pptx')
看起来,“肘部”部分是在“水平”中间创建的,而不是垂直的。有什么办法可以改变肘部线条吗?
我遇到了完全相同的问题,但我发现我可以相应地操纵坐标、旋转和翻转形状。我将形状旋转 90 度,并确保没有垂直翻转形状。然后,您需要修改 x、y、cx 和 cy,因为在旋转形状之前会考虑原始形状坐标和尺寸。 这不是最简洁的代码,但希望您能明白。
# Extract the spPr element from the connector's XML
sp = connector._element
spPr = sp.find('.//{http://schemas.openxmlformats.org/presentationml/2006/main}spPr')
# If spPr element is found, extract x, y, cx, cy values and modify them
if spPr is not None:
xfrm = spPr.find('.//{http://schemas.openxmlformats.org/drawingml/2006/main}xfrm')
if xfrm is not None:
off = xfrm.find('.//{http://schemas.openxmlformats.org/drawingml/2006/main}off')
ext = xfrm.find('.//{http://schemas.openxmlformats.org/drawingml/2006/main}ext')
# Read current values
if off is not None:
x = int(off.get('x'))
y = int(off.get('y'))
if ext is not None:
cx = int(ext.get('cx'))
cy = int(ext.get('cy'))
# Modify the attributes
new_x = x + (cx - cy) // 2
new_y = y - (cx - cy) // 2
new_cx = cy
new_cy = cx
#
xfrm.set('rot','5400000')
xfrm.set('flipV', '0')
# Update the position and size values
off.set('x', str(new_x)) # New x value
off.set('y', str(new_y)) # New y value
ext.set('cx', str(new_cx)) # New cx value
ext.set('cy', str(new_cy)) # New cy value