前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python批量将ppt转换为pdf

Python批量将ppt转换为pdf

原创
作者头像
python亦希
发布2022-01-18 16:35:54
1.3K0
发布2022-01-18 16:35:54
举报
文章被收录于专栏:python理论

将脚本跟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()

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档