feature: Picture.replace_image()
See original GitHub issueAs discussed on the mailing group (https://groups.google.com/forum/?hl=en#!topic/python-pptx/YJBzJznTTHw), there are some potential uses for replacing an image with a new one. [Think “refreshing” a PPT with updated images.]
Some working code (largely suggested by Steve) is as follows:
from pptx import Presentation
from pptx.util import Px
from PIL import Image
# inputs
PPT_TEMPLATE = 'test.pptx'
REPLACEMENT_IMG = 'pic.png'
# load preso and shape
presso = Presentation(PPT_TEMPLATE)
slide = presso.slides[0]
img = slide.shapes[0]
# get current image info
imgPic = img._pic
imgRID = imgPic.xpath('./p:blipFill/a:blip/@r:embed', namespaces=imgPic.nsmap)[0]
imgPart = slide.related_parts[imgRID]
# get info about replacement image
with open(REPLACEMENT_IMG, 'rb') as f:
rImgBlob = f.read()
rImgWidth, rImgHeight = Image.open(REPLACEMENT_IMG).size
rImgWidth, rImgHeight = Px(rImgWidth), Px(rImgHeight) # change from Px
# replace
imgPart._blob = rImgBlob
# now alter the size and position to suit that of the replacement img
# rescale sizes so image isn't stretched
widthScale = float(rImgWidth) / img.width
heightScale = float(rImgHeight) / img.height
maxScale = max(widthScale, heightScale)
scaledImgWidth, scaledImgHeight = int(rImgWidth / maxScale), int(rImgHeight / maxScale)
# center the image if it's different size to the original
scaledImgLeft = int(img.left + (img.width - scaledImgWidth)/2)
scaledImgTop = int(img.top + (img.height - scaledImgHeight)/2)
# now update
img.left, img.top, img.width, img.height = scaledImgLeft, scaledImgTop, scaledImgWidth, scaledImgHeight
Issue Analytics
- State:
- Created 9 years ago
- Reactions:3
- Comments:20 (5 by maintainers)
Top Results From Across the Web
Replace or delete a picture in Microsoft Office
From the menu that appears, select where you want to get the replacement picture from. Navigate to the image you want, and then...
Read more >Replace image - Pixelmator Pro User Guide
Use the Replace Image feature to quickly swap one image for another while preserving the layer styles, color adjustments, effects, and other nondestructive ......
Read more >Change Picture from clipboard in Word and Office
'Change Picture' lets you smoothly replace an existing image with another while keeping much the same image settings. If the clipboard item is ......
Read more >Replace Picture - UiPath Documentation Portal
List OCR/Image Activities. Container Usage ... Match Function. Protect Sheet ... Replace. Text to Left/Right. Add Data Column. Add Data Row.
Read more >Find and replace image path - GIS Stack Exchange
path.exists() (and os.path.isfile()) to test that your pathNEW actually points to a file before setting the new path to pic.sourceImage.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
+1 would like this feature
to those who wants a solution, summing up from above comments
there are two solutions
Solution 1
slide_obj -> is the slide where you want to add picture
img_location -> path to the image file
shape_obj -> existing picture shape object
shape_left, shape_top, shape_width, shape_height -> are self explanatory
Solution 2:
in this method we’re replacing blob, there’ll be a problem with this method when you have a requirement to replicate current slide in a loop.
For example, let’s say you want to replicate a slide containing image for 5 times. Each time you want to put 5 different images namely a, b, c, d, e
So, what’ll happen with this method is you’ll see the last image i.e. ‘e’ in all 5 slides. why ? because we’re replacing blob so the latest blob will be put everywhere