Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


jcherry7 at gatech

Jun 10, 2009, 11:22 AM

Post #1 of 2 (2677 views)
Permalink
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
Student
Computer Science Major (working on it)
Georgia Institute of Technology
--
http://mail.python.org/mailman/listinfo/python-list


jcherry7 at gatech

Jun 10, 2009, 12:47 PM

Post #2 of 2 (2627 views)
Permalink
Re: How do you insert an image into Powerpoint using python/win32? [In reply to]

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
To: python-list [at] python
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
Student
Computer Science Major (working on it)
Georgia Institute of Technology

--
Jonathan Cherry jcherry7 [at] gatech
Student
Computer Science Major (working on it)
Georgia Institute of Technology
--
http://mail.python.org/mailman/listinfo/python-list

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.