I've just stumbled across this, it looks extremely useful. I found some examples for manipulating slides and the like, my particular use case involves basically replacing a bunch of images within a given presentation with different image files, but I want to retain most of the metadata such as position, size, etc.
I suppose the question is a little more generic in nature, more like "what's the logical flow of this within the python-pptx framework?". Simply replacing the file pointer with the new one misses the mark for sure, but it's not obvious to me whether there are attributes for pictures that could be easily stored and re-applied, or what other approaches might make the most sense to have the code be easier to work with down the road...
any suggestions appreciated ;-)
Update: Attempted the following assignment of _blob
but it appears to not be working, or maybe I'm missing something easy?
#!/usr/bin/env python3
import pptx
import hashlib
prs = pptx.Presentation('hack.pptx')
newImgFilename = "gray.jpg"
img2 = pptx.parts.image.Image.from_file(newImgFilename)
print(hashlib.sha224(prs.slides[0].shapes[2].image._blob).hexdigest())
print(hashlib.sha224(img2._blob).hexdigest())
### these two should be different
prs.slides[0].shapes[2].image._blob = img2._blob
print(hashlib.sha224(prs.slides[0].shapes[2].image._blob).hexdigest())
### now this should be the value from img2, but it's not...
Update Jan 2023 (working code):
#!/usr/bin/python3
import pptx
smallfile = "small.jpg"
# open presentation
prs = pptx.Presentation('test.pptx')
# create new image part from new image file
new_pptx_img = pptx.parts.image.Image.from_file(smallfile)
# obviously have to figure out what image you're actually changing...
img_shape = prs.slides[0].shapes[0]
# get part and rId from shape we need to change
slide_part, rId = img_shape.part, img_shape._element.blip_rId
image_part = slide_part.related_part(rId)
# overwrite old blob info with new blob info
image_part.blob = new_pptx_img._blob
# save it
prs.save('changed.pptx')