python-3.xpowerpointpython-pptxpptxgenjs

Delete images from pptx file using python


I have one pptx file which contain 10 image(jpg). Now i want to remove some images from pptx. I have the code through which i can extract all the images present in pptx. But at same time i want to delete 2-3 images from pptx.

Can anyone help me to get the logic or any libarary to delete image in pptx using python.


Solution

  • If you use python-pptx library you can remove certain images with the following:

    from pptx import Presentation
    
    pres = Presentation("test.pptx") # name of your pptx
    slide = pres.slides[0] # or whatever slide your pictures on
    pic  = slide.shapes[0] # select the shape holding your picture and you want to remove
    
    pic = pic._element
    pic.getparent().remove(pic) # delete the shape with the picture
    
    pres.save("test.pptx") # save changes