How do you insert an image into Powerpoint using python/win32?

Cherry, Jonathan M jcherry7 at gatech.edu
Wed Jun 10 21:47:49 CEST 2009


Never mind, its just that the "choose file" option produces a file path with '/" rather than '\', and python cannot use the former.
Thanks anyway
----- Original Message -----
From: jcherry7 at gatech.edu
To: python-list at python.org
Sent: Wednesday, June 10, 2009 2:22:22 PM GMT -05:00 US/Canada Eastern
Subject: How do you insert an image into Powerpoint using python/win32?

I have python 3.0.1, and have downloaded pywin32 for python 3.x, aka build #213.
I ran win32com.client.makepy.py on Microsoft Office 12.0 Object Library and Microsoft PowerPoint 12.0 Object Library.  The output files were placed in win32com.gen_py.  I renamed them as MSO.py and MSPPT.py, respectively.  The following code is located in my c:\Python30 folder.  Thinkter is used to prompt the user to choose the image that they want to put onto the slide.  THe 10 pointed star was an experiment, to try to import something into the image.  Here is the code as I currently have it:

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)


this is the error:

Traceback (most recent call last):
  File "C:\Python30\PowerPointEditer.py", line 21, in <module>
    Pict1 = Slide1.Shapes.AddPicture(FileName=pictName, LinkToFile=False, SaveWithDocument=True, Left=100, Top=100, Width=200, Height=200)#pictName, pictName, 200, 200, 200, 200)
  File "C:\Python30\lib\site-packages\win32com\gen_py\MSPPT.py", line 8544, in AddPicture
    , Height)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, "The specified file wasn't found.", None, 0, -2147024809), None)

It seems I'm passing AddPictures the wrong varibles

Thanks.

-- 
Jonathan Cherry jcherry7 at gatech.edu
Student
Computer Science Major (working on it)
Georgia Institute of Technology

-- 
Jonathan Cherry jcherry7 at gatech.edu
Student
Computer Science Major (working on it)
Georgia Institute of Technology



More information about the Python-list mailing list