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

99封情书 提交于 2019-12-08 08:05:48

问题


I am working on powerpoints using python.pptx, where I am struggling to save certain picture from a slide to local system.Can anyone suggest me how to do it?

Until now : I am able to print the shape, But dont know how to save the picture as we do with presentation using 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 --------

Thanks in advance.


回答1:


An image depicted in a Picture shape can be accessed using its image property. The Image object provides access to detailed properties of the image, including the bytes of the image file itself.

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

So, for example:

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


来源:https://stackoverflow.com/questions/48455680/how-to-download-or-save-a-picture-from-a-powerpoint-ppt-with-python-pptx

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!