Python 尝试从目录中删除文件时出错FileNotFoundError:[WinError 2]

Python 尝试从目录中删除文件时出错FileNotFoundError:[WinError 2],python,operating-system,win32com,Python,Operating System,Win32com,我正在尝试使用以下代码从目录中删除特定文件: def PPTtoPDF(inputFileName = 'presentation.pptx', outputFileName = 'presentation.pdf', formatType = 32): try: os.remove(os.path.abspath(outputFileName)) except: pass powerpoint = comtypes.client.Cre

我正在尝试使用以下代码从目录中删除特定文件:

def PPTtoPDF(inputFileName = 'presentation.pptx', outputFileName = 'presentation.pdf', formatType = 32):
    try: 
        os.remove(os.path.abspath(outputFileName))
    except:
        pass
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible = 1
    if outputFileName[-3:] != 'pdf':
        outputFileName = outputFileName + ".pdf"
    deck = powerpoint.Presentations.Open(os.path.abspath(inputFileName))
    deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
    deck.Close()
    powerpoint.Quit()

    return os.path.abspath(outputFileName)
我得到了以下错误:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-123-c809253c599e> in <module>
----> 1 pdf_file = PPTtoPDF()

~\Documents\ppt.py in PPTtoPDF(inputFileName, outputFileName, formatType)
    129     try:
--> 130         os.remove(os.path.abspath(outputFileName))
    131     except:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 'C:\\...\\presentation.pdf'
FileNotFoundError回溯(最近一次调用)
在里面
---->1 pdf_file=PPTtoPDF()
PPTtoPDF中的~\Documents\ppt.py(inputFileName、outputFileName、formatType)
129尝试:
-->130 os.remove(os.path.abspath(outputFileName))
131除:
FileNotFoundError:[WinError 2]系统找不到指定的文件:“C:\\…\\presentation.pdf”

任何帮助都将非常感谢

您似乎不希望
outputFileName
以.pdf结尾(请参见第8+9行),所以您添加了它。使用
os.remove
您不会这样做。所以我猜可能是因为这个。调用时,
outputFileName
string打印什么?虽然它有这两行代码,但我在上面加了一个
pdf
扩展名。当我尝试运行代码(
pdf\u file=PPTtoPDF()
)时,powerpoint确实会打开并显示一个“发布”窗口,然后关闭并弹出以下错误:
COMError:(-2147188160,无,(“应用程序(未知成员)):请求无效。无法在此事件处理程序中执行此操作。“,“Microsoft PowerPoint”,“0,无”)
。当我尝试调用这个pdf_文件var时,它说它没有定义。有什么想法吗?