python:读fi时出错

2024-06-23 03:35:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用Windows和Jupyter编写python代码,当我使用package时comtypes.客户端,它不稳定,有时有效,有时不工作。我正在做的是尝试将pptx文件转换为pdf文件。这是我的代码:

import comtypes.client

def PPTtoPDF(inputFileName, outputFileName, formatType = 32):
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible = 1

    if outputFileName[-3:] != 'pdf':
        outputFileName = outputFileName + ".pdf"
    deck = powerpoint.Presentations.Open(inputFileName)
    deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
    deck.Close()
    powerpoint.Quit()

PPTtoPDF('07-20 contact PPT.pptx','./Reports/07-20 contact result',32)

错误是:

^{pr2}$

我不认为这段代码有什么问题,因为它运行了好几次,所以可能是因为计算机环境或其他原因。有谁能告诉我如何使代码稳定,或者有更好的方法将PPTX转换成python中的PDF?在

谢谢大家~


Tags: 文件代码clientpdfwindowscontactjupyterdeck
1条回答
网友
1楼 · 发布于 2024-06-23 03:35:37

我收到了同样的错误,我有一台64位的Windows PC。我的解决方案包括使用以下命令指定文件路径:

# Open the ppt file path. sys.argv[1] is the ppt name.
mypath = os.path.abspath(__file__)
mydir = os.path.dirname(mypath)
file_input = os.path.join(mydir, sys.argv[1])

# create the pdf output file path and call your function 
file_output = os.path.join(mydir, sys.argv[1][:-4] + "pdf")
PPTtoPDF(file_input, file_output)

相关问题 更多 >

    热门问题