【Python批量将ppt转换为pdf】将脚本跟PPT文件放置在同一个文件夹下,运行脚本,能够批量地将微软Powerpoint文件(.ppt或.pptx)转换为pdf格式。
1 import comtypes.client
2 import os
3
4 def init_powerpoint():
5powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
6powerpoint.Visible = 1
7return powerpoint
8
9 def ppt_to_pdf(powerpoint, inputFileName, outputFileName, formatType = 32):
10if outputFileName[-3:] != 'pdf':
11outputFileName = outputFileName + ".pdf"
12deck = powerpoint.Presentations.Open(inputFileName)
13deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
14deck.Close()
15
16 def convert_files_in_folder(powerpoint, folder):
17files = os.listdir(folder)
18pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
19for pptfile in pptfiles:
20fullpath = os.path.join(cwd, pptfile)
21ppt_to_pdf(powerpoint, fullpath, fullpath)
22
23 if __name__ == "__main__":
24powerpoint = init_powerpoint()
25cwd = os.getcwd()
26convert_files_in_folder(powerpoint, cwd)
27powerpoint.Quit()
推荐阅读
- 【python笔记】使用python的pyquery简单爬取数据demo
- mysql视图简介与使用
- 爬虫程序部署后常见问题整理
- Python_计算机基础
- [Python] Use Python Classes
- python 处理json
- 【python】python文件处理
- 【Python】Python数组
- Python 循环的本质就是一段代码懒得重复写