Python批量將ppt轉換為pdf


將腳本跟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()

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM