2015-09-06 111 views
3

enter image description here我有一個由matplotlib生成的圖,然後將它保存爲.png,然後使用pptx模塊將其放置在PPT文件中。 我想在我的PPT文件中添加圖片的邊框可以任何一個請幫我代碼.. ??在由python和matplotlib生成的PPTX中爲圖片(圖)添加邊框

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') 
+0

當前的解決方案是否裁剪'test.png'的邊框?它應該是什麼樣子,目前在powerpoint中是什麼樣的?在我看來,在添加到演示文稿之前更改matplotlib中的圖像會更容易 –

+0

剛剛添加圖片Smith ..看看它.. – MoChen

回答

1

在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) 
+0

嗨Scanny它可以工作,但略有修改。而不是line.color = RGBColor(128,128,128),我們需要使用line.color.rgb = RGBColor(128,128,128) – MoChen

+0

啊,是的,當然,我記得現在你提到它:)我更新了代碼示例反映。 – scanny

0

形狀(在這種情況下,你的圖片對象)有一個.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