如何使用 python pptx 从 powerpoint ppt 下载或保存图片

how to download or save a picture from a powerpoint ppt with python pptx

我正在使用 python.pptx 制作 powerpoints,我很难将幻灯片中的某些图片保存到本地 system.Can 有人建议我怎么做吗?

直到现在:我可以打印形状,但不知道如何保存图片,就像我们使用 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)