Search code examples
pythonpython-3.xpowerpointwin32compowerpoint-2007

Python: Pause for-loop while application closes


I have a script that I would like to bulk edit powerpoint files with. If I edit files one by one with it, it works great. If I bulk edit them, it fails. I assume this is because the application is not closing before the next file attempts to load, but I could, and most likely am, wrong.

The code:

import win32com.client, sys, glob


folder = (glob.glob('*.ppt'))

print("="*20)
print(folder)
print("="*20)

if folder:
    for files in folder:
        print("Current File: " + files)
        try:
            Application = win32com.client.Dispatch("PowerPoint.Application")
            Application.Visible = True
            Presentation = Application.Presentations.Open("c:/pptpy/testfolder/" + files)
            for Slide in Presentation.Slides:
                for Shape in Slide.Shapes:
                    try:
                        Shape.TextFrame.TextRange.Font.Name = "Arial"
                        Shape.TextFrame.TextRange.Font.Size = "14"
                        Shape.TextFrame.TextRange.Font.Color.RGB = "000000"
                    except:
                        pass
            Presentation.Save()
            Application.Quit()
                #Adding a time.sleep(1) here pauses the Application.Quit()
        except:
            print("Error in: " + files)
            pass

The error (when not passing exceptions):

Traceback (most recent call last):
  File "C:\pptpy\testfolder\convert.py", line 19, in <module>
    for Shape in Slide.Shapes:
  File "C:\Python33\lib\site-packages\win32com\client\dynamic.py", line 247, in __getitem__
    return self._get_good_object_(self._enum_.__getitem__(index))
  File "C:\Python33\lib\site-packages\win32com\client\util.py", line 37, in __getitem__
    return self.__GetIndex(index)
  File "C:\Python33\lib\site-packages\win32com\client\util.py", line 53, in __GetIndex
    result = self._oleobj_.Next(1)
pywintypes.com_error: (-2147023174, 'The RPC server is unavailable.', None, None)

Details:

Python3.3

Powerpoint2007

If you need any more details, I would be happy to provide them! Thanks!


Solution

  • Try something like this (building on previous question). You should really invest time in designing your code before you ask questions like this:

    import win32com.client
    import sys # <- obsolete not used
    import os
    import glob # style guide one import per line
    
    
    Application = win32com.client.Dispatch("PowerPoint.Application")
    Application.Visible = True
    
    ppt_files = glob.glob('*.ppt')
    
    for file in ppt_files:
        file = os.path.abspath(file)
        Presentation = Application.Presentations.Open(file)
        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()
        Presentation.Close()
    
    Application.Quit()