0

I am putting together a python script that will "clean up" a PowerPoint file's font-face, font-colors, font-size, etc. I found a snippet of code that does what I need it to do, however it seems to break as soon as it meets an image in a slide.

    import win32com.client, sys
Application = win32com.client.Dispatch("PowerPoint.Application")
Application.Visible = True
Presentation = Application.Presentations.Open(sys.argv[1])
for Slide in Presentation.Slides:
    for Shape in Slide.Shapes:
        Shape.TextFrame.TextRange.Font.Name = "Arial"
        Shape.TextFrame.TextRange.Font.Size = "12"
        Shape.TextFrame.TextRange.Font.Color.RGB = "000000"
Presentation.Save()
Application.Quit()

EDIT: Copy and pasted the wrong code... removed non-working if statement.

This all runs just fine, cleaning up silly looking fonts and colors until it hits its first image. Then it breaks and I am presented with this:

Traceback (most recent call last):
  File "c:/pptpy/convert.py", line 7, in <module>
    Shape.TextFrame.TextRange.Font.Name = "Arial"
  File "C:\Python33\lib\site-packages\win32com\client\dynamic.py", line 511, in __getattr__
    ret = self._oleobj_.Invoke(retEntry.dispid,0,invoke_type,1)
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, 'The specified value is out of range.', None, 0, -2147024809), None)

If I remove all images (not shapes) from the file and run the script, it works great and I'm left with a decent powerpoint. The images are pretty important, however.

Details:

Python 3.3

PowerPoint 2007

Script to hopefully convert batches of 200-300 PPT(x) at a time, once finished.

If you need any more details, let me know! Thanks!

Eric
  • 323
  • 4
  • 20
  • the code you display can not work since if TextFrame is True: will not work as **TextFrame** is not defined – joojaa Jun 04 '13 at 10:31
  • I accidentally posted work-in-progress code that didn't work. I've updated the question with the code that should work. – Eric Jun 04 '13 at 10:52

2 Answers2

1

While your code is not actually working replacing your if with a try catch works fine. This may not be a elegant method tough.

import win32com.client, sys
Application = win32com.client.Dispatch("PowerPoint.Application")
Application.Visible = True
Presentation = Application.Presentations.Open(sys.argv[1])
for Slide in Presentation.Slides:
    for Shape in Slide.Shapes:
        try:
            Shape.TextFrame.TextRange.Font.Name = "Arial"
            Shape.TextFrame.TextRange.Font.Size = "12"
            Shape.TextFrame.TextRange.Font.Color.RGB = "000000"
        except:
            pass
Presentation.Save()
Application.Quit()  

What probably happens to you is that the if is always true since an object qualifies as true. Try asking length of the object greater than 0 or something similar.

joojaa
  • 4,354
  • 1
  • 27
  • 45
  • Thank you! That works as intended. I still have a basic grasp of python and didn't even know about try/except. – Eric Jun 04 '13 at 10:51
1

You can surround the active code with something like:

If Shape.HasTextFrame Then
   If Shape.TextFrame.HasText Then
      ' do your stuff
   End If
End If
Steve Rindsberg
  • 14,442
  • 1
  • 29
  • 34