温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

批量将ppt转换为pdf的Python代码 只要27行!

发布时间:2020-08-26 00:11:06 来源:脚本之家 阅读:137 作者:zhusongziye 栏目:开发技术

这是一个Python脚本,能够批量地将微软Powerpoint文件(.ppt或者.pptx)转换为pdf格式。

使用说明

1、将这个脚本跟PPT文件放置在同一个文件夹下。
2、运行这个脚本。

全部代码

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 + ".pdf"
 deck = powerpoint.Presentations.Open(inputFileName)
 deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
 deck.Close()

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()

源码地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI