本文共 1132 字,大约阅读时间需要 3 分钟。
import comtypes.clientimport osdef init_powerpoint(): powerpoint = comtypes.client.CreateObject("Powerpoint.Application") powerpoint.Visible = 1 return powerpointdef ppt_to_pdf(powerpoint, inputFileName, outputFileName, formatType=32): if outputFileName[-3:] != 'pdf': outputFileName = outputFileName + '.pdf' deck = powerpoint.Presentations.Open(inputFileName) deck.SaveAs(outputFileName, formatType) deck.Close()def convert_files_folder(powerpoint, folder): files = os.listdir(folder) pptfiles = [f for f in files if f.endswith(('.ppt', '.pptx'))] for pptfile in pptfiles: fullpath = os.path.join(folder, pptfile) ppt_to_pdf(powerpoint, fullpath, fullpath)if __name__ == '__main__': powerpoint = init_powerpoint() cwd = os.getcwd() convert_files_folder(powerpoint, cwd) powerpoint.Quit() 该代码用于将PowerPoint文件转换为PDF格式,适用于批量处理。代码定义了三个主要函数:
init_powerpoint():初始化PowerPoint应用程序并设置其可见性。ppt_to_pdf():将指定文件格式的PowerPoint文件保存为PDF文件,支持设置输出格式。convert_files_folder():遍历指定文件夹,找到所有PowerPoint文件并进行批量转换。代码的主要功能包括文件遍历、文件格式转换以及PowerPoint应用程序的操作,适合用于自动化处理和批量转换任务。
转载地址:http://keafk.baihongyu.com/