0

我有一个脚本,我想用它来批量编辑 powerpoint 文件。如果我用它一个一个地编辑文件,它会很好用。如果我批量编辑它们,它会失败。我认为这是因为在下一个文件尝试加载之前应用程序没有关闭,但我可能而且很可能是错误的。

编码:

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

错误(当不传递异常时):

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)

细节:

Python3.3

PowerPoint2007

如果您需要更多详细信息,我很乐意提供!谢谢!

4

1 回答 1

1

尝试这样的事情(基于上一个问题)。在你问这样的问题之前,你真的应该花时间设计你的代码:

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()
于 2013-06-05T07:02:18.607 回答