原理:
Python調(diào)用office軟件,將ppt另存為pdf帖努,當然還支持Excel和word轉(zhuǎn)pdf了观话,請自行研究
前提:
1.安裝了office
2.安裝了python、pip
直接上代碼
import comtypes.client
import os
def init_powerpoint():
powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
powerpoint.Visible = 1
return powerpoint
def ppt_to_pdf(powerpoint, inputFileName, outputFileName, formatType = 32):
if outputFileName[-3:] != 'pdf':
outputFileName = outputFileName.replace(".pptx","").replace(".ppt","") + ".pdf"
deck = powerpoint.Presentations.Open(inputFileName)
deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
deck.Close()
print('轉(zhuǎn)換%s文件完成'%outputFileName)
def convert_files_in_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(cwd, pptfile)
ppt_to_pdf(powerpoint, fullpath, fullpath)
if __name__ == "__main__":
powerpoint = init_powerpoint()
cwd = os.getcwd()
convert_files_in_folder(powerpoint, cwd)
powerpoint.Quit()
使用方法
1.安裝依賴庫
pip install comtypes
2.運行
將batch_ppt_to_pdf.py文件和ppt文件放在同一文件夾下宇姚,打開cmd運行
python batch_ppt_to_pdf.py