我一直在尝试使用 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 未找到,对象不存在等。