如何使用Python PPTX从PowerPoint PPT下载或保存图片



我正在使用python.pptx制作PowerPoint,我正在努力将幻灯片中的某些图片保存到本地系统。谁能建议我怎么做?

直到现在:我能够打印形状,但不知道如何保存图片,就像我们使用prs.save进行演示一样。

prs =Presentation('mypath/myPowerpoint.pptx')
slide2 = prs.slides[1]       #i want to save picture in slide 2
pic = slide2.shapes[4]       # i have check shape 5 is the picture
print(pic.name)              # i am able to print the picture name
pic.save('Mypic.jpg')        #------ this didn't work --------

提前谢谢。

可以使用

image 属性访问以Picture形状描述的图像。Image 对象提供对图像详细属性的访问,包括图像文件本身的字节数。

http://python-pptx.readthedocs.io/en/latest/api/shapes.html#pptx.shapes.picture.Picture.image

http://python-pptx.readthedocs.io/en/latest/api/image.html#pptx.parts.image.Image

因此,例如:

with open('mypic.jpg', 'wb') as f:
    f.write(pic.image.blob)

最新更新