3

I have been tasked with inserting and re-sizing several hundred images to a powerpoint. I need to use a particular source format that is similar to other power points used by our company.

I have been playing around with activepython's win32com API's and I have figured out how to open up a file and create a blank slide.

My question is how would I go about inserting an image and resizing it to whatever size I need(the images will be the only thing on each page). Also I am trying to use my company's theme for the background and title page but this is not as important as getting the images on page and re-sized.

any help would be greatly appreciated, thanks!

1
  • Thanks everyone! very helpful, just processed 500+ images in less then a minute. Commented Sep 2, 2011 at 17:17

4 Answers 4

2

I got this from the page Xavier referred to:

Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200)

That will work if your original images are square; otherwise, it will distort them.

Better to specify -1 for the width and height. Then PPT will insert them at their "natural" size (whatever PPT might decide that is ... not important for present purposes). Then you can read the shape's size to determine its aspect ratio and make sure that stays constant if you change the size or you can set the shape's .LockAspectRatio property to true and adjust either height or width and the other will auto adjust to maintain the aspect ratio.

Sign up to request clarification or add additional context in comments.

1 Comment

No matter how i try to specify the filename (with complete directory, or name only after specifying working directory), I'm getting an error message saying file not found. Any Idea what could cause this? The directory is correct and the png image is there. I'm using Python 3 on windows.
2

You should use Shapes.AddPicture as described here (archived version of this broken link):

from tkinter import *
import tkinter.filedialog as tkFileDialog
import win32com.client # middleman/translator/messanger between windows and python
import win32com.gen_py.MSO as MSO # contains constants refering to Microsoft Office Objects
import win32com.gen_py.MSPPT as MSPPT # contains constants refering to Microsoft Office Power Point Objects
g = globals() # a dictonary of global vlaues, that will be the constants of the two previous imports
for c in dir(MSO.constants): g[c] = getattr(MSO.constants, c) # globally define these
for c in dir(MSPPT.constants): g[c] = getattr(MSPPT.constants, c)
Application = win32com.client.Dispatch("PowerPoint.Application")
Application.Visible = True # shows what's happening, not required, but helpful for now
Presentation = Application.Presentations.Add() # adds a new presentation
Slide1 = Presentation.Slides.Add(1, ppLayoutBlank) # new slide, at beginning
TenptStr = Slide1.Shapes.AddShape(msoShape10pointStar, 100, 100, 200, 200)
pictName = tkFileDialog.askopenfilename(title="Please Select the Image you wish to load")
print(pictName)
Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200)

Note that the "choose file" option produces a file path with '/" rather than '' and you may need to replace the former by the latter.

Comments

1

Firstly I would resize them first before insertion using PIL python imaging library. In general mucking about with any office image processing has been uniformly awful for me so I would not recommend it.

secondly the object model examples in VBA found in PowerPoint help or online are usually all you need to get a long way

from memory the insert was pretty easy -

ppt.slide.ImageFromFile("path")

however exact positioning I do not remember and I am afraid I don't have ppt here to try out. If I get achance later I will post some references

Good luck with the docs

Comments

0

I agree with the accepted answer. I would like to point out that I needed to prefix the FileName string with an 'r' to treat the backslashes as literal characters. Escaping the backslashes by replacing single backslashes with double also works. @vestland this may be helpful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.