将脚本跟PPT文件放置在同一个文件夹下,运行脚本,能够批量地将微软Powerpoint文件(.ppt或.pptx)转换为pdf格式。
1 import comtypes.client 2 import os 3 4 def init_powerpoint(): 5 powerpoint = comtypes.client.CreateObject("Powerpoint.Application") 6 powerpoint.Visible = 1 7 return powerpoint 8 9 def ppt_to_pdf(powerpoint, inputFileName, outputFileName, formatType = 32): 10 if outputFileName[-3:] != 'pdf': 11 outputFileName = outputFileName + ".pdf" 12 deck = powerpoint.Presentations.Open(inputFileName) 13 deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf 14 deck.Close() 15 16 def convert_files_in_folder(powerpoint, folder): 17 files = os.listdir(folder) 18 pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))] 19 for pptfile in pptfiles: 20 fullpath = os.path.join(cwd, pptfile) 21 ppt_to_pdf(powerpoint, fullpath, fullpath) 22 23 if __name__ == "__main__": 24 powerpoint = init_powerpoint() 25 cwd = os.getcwd() 26 convert_files_in_folder(powerpoint, cwd) 27 powerpoint.Quit()