问题
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:
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.
回答2:
you should use Shapes.AddPicture as described here http://www.gossamer-threads.com/lists/python/python/754236
回答3:
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
回答4:
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
来源:https://stackoverflow.com/questions/7284658/insert-images-to-powerpoint-slide-using-python-win32com-client