I've been racking my brain over this for a few days now. I'm attempting to write a program in Python 3.1 / PyQt 4.7.5 that will open a Powerpoint, go through every shape on every slide, determine if it is a picture and then save that picture to the specified directory with the correct name and extension. So far everything works. The GUI is ok, the logic for my code is fine and it determines the shapes as it should... the only problem is when I try to export the shapes. When it gets to actually exporting the shapes I get this traceback:
Traceback (most recent call last):
File "C:\Documents and Settings\root\My Documents\Projects\PyQT\PPT Photo Exporter\Methods.py", line 94, in exportPhotos
Shape.Export(self.currentOutDir.join(self.counterStr), 13)
File "C:\Python31\lib\site-packages\win32com\MSOPPT.py", line 7643, in Export
, Filter, ScaleWidth, ScaleHeight, ExportMode)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147467259), None)
The method that I'm using is:
def exportPhotos(self):
self.currentFileName = self.view.pptPath.text()
self.currentOutDir = self.view.outPath.text()
g = globals()
for c in dir(self.msoModule.constants): g[c] = getattr(self.msoModule.constants, c)
for c in dir(self.pptModule.constants): g[c] = getattr(self.pptModule.constants, c)
if self.currentFileName == '':
QtGui.QMessageBox.information(self.view, "Empty path",
"Sorry, but it looks like the file path is empty!")
return
if self.currentOutDir == '':
QtGui.QMessageBox.information(self.view, "Empty path",
"Sorry, but it looks like you didn't type in the output directory!")
return
self.loadedPPT = win32com.client.Dispatch("Powerpoint.Application")
self.loadedPPT.Visible=1
self.presentation = self.loadedPPT.Presentations.Open(self.currentFileName)
self.counter = 0
for Slide in self.presentation.Slides:
for Shape in Slide.Shapes:
if Shape.Type == 13: #The 13 is the enum value for ppShapeFormatJPG
self.counter =+ self.counter + 1
self.counterStr = "/"+str(self.counter)+".jpg"
print(self.currentOutDir + self.counterStr)
Shape.Export(self.currentOutDir.join(self.counterStr), 13)
else:
print("not a photo")
self.presentation.Close()
self.loadedPPT.Quit()
The reference is from the module generated by PyWin for Powerpoint COM objects:
def Export(self, PathName=defaultNamedNotOptArg, Filter=defaultNamedNotOptArg, ScaleWidth=0, ScaleHeight=0
, ExportMode=1):
return self._oleobj_.InvokeTypes(2023, LCID, 1, (24, 0), ((8, 1), (3, 1), (3, 49), (3, 49), (3, 49)),PathName
, Filter, ScaleWidth, ScaleHeight, ExportMode)
The 13 in the Shape.Export method is the enum value for ppShapeFormatJPG and currentOutDir is the directory specified by the user in the GUI. The print statements are being used to verify my logic and correct naming scheme of the exported files.
MSDN doc:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.powerpoint.shape.export.aspx
Everything seems to be in the right place, but it's throwing some unnamed exception. Any help you are able to give would be fantastic. The sources for Pywin problems are few and far between and I haven't been able to find much. I've found some help on python-forum.org which helped me figure out the enum values and such, but so far no luck on this exception. Any help you are able to give would be very much appreciated.