如何使用py32win自动打开带有密码的powerpoint文件?

2024-05-15 12:28:39 发布

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

对于工作,我需要将给定文件夹中的所有ppt/pptx和doc/docx文件转换为pdf。但是,有些文件受密码保护。到目前为止,我有以下代码:

import os
import win32com
import win32com.client

print("Please set the directory you want to scrape files from:")
FOLDER_PATH = input()


def PPTtoPDF(inputFileName, outputFileName, formatType=32):
    powerpoint = win32com.client.Dispatch("Powerpoint.Application")
    if inputFileName[-3:] == "ppt":
        outputFileName = outputFileName[:-4] + ".pdf"
    if inputFileName[-4:] == "pptx":
        outputFileName = outputFileName[:-5] + ".pdf"
    if (inputFileName[-3:] == "ppt") | (inputFileName[-4:] == "pptx"):
        try:
            deck = powerpoint.Presentations.Open(inputFileName, WithWindow=False)
            deck.SaveAs(outputFileName, formatType)  # formatType = 32 for ppt to pdf
            deck.Close()
            os.remove(inputFileName)
        except:
            print("PPT2PDF: There's an error" + inputFileName)

之后,我有另一个函数,它从我设置的目录中获取文件名,将其放入一个列表中,然后在列表中循环运行ppttopf函数

上面的功能可以工作,但每次有密码保护的文档时,我都必须手动输入密码

我尝试了以下修复:

deck = powerpoint.Presentations.Open(inputFileName, WithWindow=False, Password="password1234")

但这就变成了一个错误:

TypeError: Open() got an unexpected keyword argument 'Password'

WithWindow参数也经常发生类似的错误,我不知道这是为什么。有时有效,有时无效

我也试过了

deck = powerpoint.Presentations.Open(inputFileName, False, True, None, "password1234")

这是在How to open a password protected excel file using python?中提出的 但它返回以下错误:

TypeError: Open() takes from 1 to 5 positional arguments

我不确定出了什么问题。编码绝对不是我的专长,py32win的文档似乎很稀少,而且技术性很强。欢迎提供任何帮助/建议

谢谢


Tags: toimportifpdfopenwin32comdeckppt