原理:
Python调用office软件,将ppt另存为pdf,当然还支持Excel和word转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('转换%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