@jhludwig: This should get it done as a temporary workaround. I'll leave this case open as a feature request to get the general case shapes.remove(shape) capability built. #843
@jhludwig: This should get it done as a temporary workaround. I'll leave this case open as a feature request to get the general case shapes.remove(shape) capability built.
Let me know how you go :)
from pptx import Presentation
from pptx.oxml import qn
def image_part_rId(picture):
"""
Return relationship id, e.g. 'rId9' corresponding to the image part
referenced by the picture shape *picture*.
"""
blip_elm = picture._element.blipFill[qn('a:blip')]
return blip_elm.get(qn('r:embed'))
def remove_picture(slide, picture):
"""
Remove *picture* shape from *slide*.
"""
# remove relationship to image part
rId = image_part_rId(picture)
remove_rel_with_rId(slide, rId)
# remove <pic> element
pic_elm = picture._element
spTree = pic_elm.getparent()
spTree.remove(pic_elm)
# remove picture from shapes
slide.shapes._values.remove(picture)
def remove_rel_with_rId(slide, rId):
"""
Remove the relationship identified by *rId* from *slide*.
"""
# get relationships list
rels = slide._relationships._values
# delete relationship with matching rId
for rel in rels:
if rel._rId == rId:
rels.remove(rel)
break
prs = Presentation('contains_picture.pptx')
slide = prs.slides[0]
picture = slide.shapes[0]
remove_picture(slide, picture)
prs.save('out.pptx')
@jhludwig: This should get it done as a temporary workaround. I'll leave this case open as a feature request to get the general case shapes.remove(shape) capability built.
Let me know how you go :)
Originally posted by @scanny in https://github.com/scanny/python-pptx/issues/41#issuecomment-25602677