gpt4 book ai didi

python - 我们如何使用不同的 win32com 实例创建多个 PDF 文件?

转载 作者:行者123 更新时间:2023-12-02 08:27:56 24 4
gpt4 key购买 nike

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

最佳答案

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

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

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

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

关于python - 我们如何使用不同的 win32com 实例创建多个 PDF 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60309623/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com