gpt4 book ai didi

python - 使用 python pptx 模块在 PowerPoint 中插入一行

转载 作者:行者123 更新时间:2023-12-01 01:46:12 26 4
gpt4 key购买 nike

以下代码行展示了如何使用 python 在 PowerPoint 幻灯片中绘制一条线。然而,下面列出的以英寸为单位的四个参数仅取正值,因此无法向后绘制。还有其他方法可以在 PowerPoint 幻灯片中画线吗?

from pptx.enum.shapes import MSO_SHAPE
line1 = slide.shapes.add_shape(MSO_SHAPE.LINE_INVERSE, Inches(6), Inches(6), Inches(1), Inches(2))

最佳答案

您可以使用连接器对象与形状对象来绘制线条(形状需要 x、y、高度和宽度,而 powerpoint 无法处理负高度)

来自docs

Lines are a sub-category of auto shape that differ in certain properties and behaviors. In particular, they have a start point and end point in addition to extents (left, top, width, height).

Connectors are based on the element and have one of a handful of different preset geometry values, such as line. Freeform connectors, despite the name, are not connectors, and are a custom geometry shape based on the p:sp element.

Connectors can be “connected” to an auto shape such that a connected end point remains connected when the auto shape is moved. This relies on a concept of “connection points” on the auto shape. These connections points are preset features of the auto shape, similar to how adjustment points are pre-defined. Connection points are identified by index.

使用下面的代码可以绘制一条从 x = 4 英寸到 x= 2 英寸的直线。

from pptx.enum.shapes import MSO_CONNECTOR
from pptx import Presentation


# Make sure you have a presentation called test1.pptx in your working directory


prs = Presentation(pptx='test1.pptx')
slide = prs.slides.add_slide(prs.slide_layouts[1])
#shapes.add_connector(MSO_CONNECTOR.STRAIGHT, start_x, start_y, end_x, end_y


line1=slide.shapes.add_connector(MSO_CONNECTOR.STRAIGHT, Inches(4), Inches(2), Inches(1), Inches(2))

prs.save('test2.pptx')

关于python - 使用 python pptx 模块在 PowerPoint 中插入一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51311069/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com