I have been trying to create multiple pdf files using DispatchEx but when I try to test my code, it creates only first pdf file while all other requests fail with strange errors. What am I doing wrong and/or how can I effectively handle multiple clients calls simultaneously to generate their respective pdf files they request?
Here is that part of my code:
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 - The code successfully creates as many ppt copies (using shutil) as requests sent to it, but the win32com gives error when multiple requests are made in short interval of time, like shape.AlternativeText not found, object does not exist etc.