在 python 和 matplotlib 生成的 PPTX 中为图片(绘图)添加边框

Adding a border to picture(plot) in a PPTX generated by python and matplotlib

我有一个由 matplotlib 生成的图,然后我将其保存为 .png,然后使用 pptx 模块将其放在 PPT 文件中。 我想在我的 PPT 文件中添加图片的边框,谁能帮我提供代码..??

from pptx.util import Inches
from pptx import Presentation

prs = Presentation('dashboard.pptx')
left = Inches(0.5)
top = Inches(1)
slide = prs.slides.add_slide(prs.slide_masters[0].slide_layouts[2])
pic = slide.shapes.add_picture('test.png',left, top,width =None ,height =None)
prs.save('dashboard_new.pptx')

形状(在本例中为您的 pic 对象)有一个 .Line 属性 控制形状周围的边框。下面是如何在 VBA 中添加行的示例。在您的情况下,您将修改 pic 对象的相同 .Line 属性:

Sub AddBorder()

    Dim oSh As Shape

    ' this assumes that the current shape is selected
    ' in other cases, you'd work with an object reference
    ' generated when you added the shape
    Set oSh = ActiveWindow.Selection.ShapeRange(1)

    With oSh
        ' if you don't set the line to be visible,
        ' you get odd results
        .Line.Visible = msoTrue
        .Line.ForeColor.RGB = RGB(255, 0, 0)
        .Line.Weight = 6    ' in points
    End With

End Sub

python-pptx 中的 Picture 对象有一个 line 属性,提供对边框属性的访问:

所以代码应该是这样的:

from pptx.dml.color import RGBColor

line = pic.line
line.color.rgb = RGBColor(0xFF, 0x00, 0x00)
line.width = Inches(0.1)