3

我一直在尝试使用 DispatchEx 创建多个 pdf 文件,但是当我尝试测试我的代码时,它只创建第一个 pdf 文件,而所有其他请求都失败并出现奇怪的错误。我在做什么错和/或如何有效地同时处理多个客户呼叫以生成他们各自请求的 pdf 文件?

这是我的代码的那一部分:

            rundate = "Quote_{:%d%m%Y_%H%M%S%f}".format(datetime.now())
            pythoncom.CoInitialize()
            FILENAME = "D:/packages/abc.pptx"
            APPLICATION = win32com.client.DispatchEx("PowerPoint.Application")
            APPLICATION.Visible = True
            path_ppt = shutil.copy(FILENAME, "D:/{0}.pptx".format(rundate))
            PRESENTATION = APPLICATION.Presentations.Open(path_ppt)
            Slide1 = PRESENTATION.Slides(1)
            Shape1 = Slide1.Shapes(1)
            print(Shape1.AlternativeText)
            for shape in Slide1.Shapes:
                if shape.AlternativeText:
                    print(shape.AlternativeText)
                if shape.HasTextFrame:
                    shape.TextFrame.TextRange.Replace(FindWhat="#abc",ReplaceWhat="THAILAND", WholeWords=False)
                if shape.AlternativeText == "1":
                    shape.Fill.UserPicture("D:/1.jpg")
                if shape.AlternativeText == "2":
                    shape.Fill.UserPicture("D:/2.jpg")
                if shape.AlternativeText == "3":
                    shape.Fill.UserPicture("D:/3.jpg")
            PATH_TO_PDF = "{0}{1}{2}".format(r'd:/',rundate,'.pdf')
            PRESENTATION.SaveAs(PATH_TO_PDF, 32)
            APPLICATION.Quit()
            PRESENTATION.Close()
            PRESENTATION =  None
            APPLICATION = None
            os.remove(path_ppt)

PS - 代码成功创建了与发送给它的请求一样多的 ppt 副本(使用 shutil),但是当在短时间内发出多个请求时,win32com 会出错,例如 shape.AlternativeText 未找到,对象不存在等。

4

2 回答 2

1

您可以通过在单独的进程中运行此代码来解决 APPLICATION 退出问题。(忽略同时调用时使用硬编码文件名的风险)。

在您的项目包中创建另一个模块

powerpointer.py

import shutil
import sys
import subprocess as subp

def do_powerpoint(filename):
   """run external copy of self to do powerpoint stuff"""
   # sys.executable is the python.exe you are using, __file__ is the
   # path to this module's source and filename you pass in
   return subp.call([sys.executable, __file__, filename])

def _do_powerpoint(filename):
    rundate = "Quote_{:%d%m%Y_%H%M%S%f}".format(datetime.now())
    pythoncom.CoInitialize()
    APPLICATION = win32com.client.DispatchEx("PowerPoint.Application")
    APPLICATION.Visible = True # I think False is better so you dont see it pop up
    path_ppt = shutil.copy(filename, "D:/{0}.pptx".format(rundate))
    PRESENTATION = APPLICATION.Presentations.Open(path_ppt)
    Slide1 = PRESENTATION.Slides(1)
    Shape1 = Slide1.Shapes(1)
    print(Shape1.AlternativeText)
    for shape in Slide1.Shapes:
        if shape.AlternativeText:
            print(shape.AlternativeText)
        if shape.HasTextFrame:
            shape.TextFrame.TextRange.Replace(FindWhat="#abc",ReplaceWhat="THAILAND", WholeWords=False)
        if shape.AlternativeText == "1":
            shape.Fill.UserPicture("D:/1.jpg")
        if shape.AlternativeText == "2":
            shape.Fill.UserPicture("D:/2.jpg")
        if shape.AlternativeText == "3":
            shape.Fill.UserPicture("D:/3.jpg")
    PATH_TO_PDF = "{0}{1}{2}".format(r'd:/',rundate,'.pdf')
    PRESENTATION.SaveAs(PATH_TO_PDF, 32)
    PRESENTATION.Close()
    APPLICATION.Quit()
    os.remove(path_ppt)

if __name__ == "__main__":
    _do_powerpoint(sys.argv[1]) # will raise error if no parameters

现在,在您的主代码中,import powerpointer并根据需要调用powerpointer.do_powerpoint(filename)。它将作为脚本运行自己的模块,并且在该实例中您将只有 1 个应用程序对象。

于 2020-02-19T23:55:22.707 回答
0

经过几个小时的工作,我能够通过引入以下更改来解决这个问题:

1) 删除

APPLICATION.Visible = True 

2) 改变

APPLICATION.Presentations.Open(path_ppt) 

PRESENTATION = APPLICATION.Presentations.Open(path_ppt, WithWindow=False, ReadOnly=False) 

3) 删除

APPLICATION.Quit()
PRESENTATION.Close()
PRESENTATION =  None
APPLICATION = None
os.remove(path_ppt) 

问题是,如果我使用

APPLICATION.Quit()
PRESENTATION.Close()
PRESENTATION =  None
APPLICATION = None
os.remove(path_ppt) 

在代码中并同时进行呼叫时,它给出了“呼叫被被呼叫者拒绝”错误。当我删除这些代码行时,它能够发布与生成的 PowerPoint 一样多的 PDF,但 PowerPoint 实例仍然存在,文件副本也仍然存在,占用了系统资源。使用 WithWindow = False 有助于在 PDF 发布后自动关闭 PowerPoint 实例。剩下的唯一问题是剩下的 PowerPoint 文件副本,当系统在您的客户端空闲时,可以在一天结束时将其删除。

PS - 我正在使用 ExportAsFixedFormat 而不是 SaveAs for PDF PRESENTATION.ExportAsFixedFormat(PATH_TO_PDF, 32, PrintRange=None)

于 2020-02-21T07:36:25.960 回答