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

yipeiwu_com5年前Python基础

这是一个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()

源码地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Pytorch 定义MyDatasets实现多通道分别输入不同数据方式

最近在做一个项目,用双通道神经网络,每个通道输入不同数据训练,具有相同label。开始没想到如何实现,网上很多例子都是单通道,即便找到双通道的例子,两个通道的输入也相同。 最后,终于想到...

python将秒数转化为时间格式的实例

1、转化成时间格式 seconds =35400 m, s = divmod(seconds, 60) h, m = divmod(m, 60) print("%d:%02d:%02...

python 提取tuple类型值中json格式的key值方法

标题比较麻烦,都有些叙述不清;昨天下午在调试接口框架的时候,遇到了一个问题是这样的: 使用python 写了一个函数,return 了两个返回值比如 return a,b 于是返回的a,...

python 执行文件时额外参数获取的实例

如下所示: def usage(): print(' * usage:') print(' * -c [val] : exporter_conf filepath, def...

Python函数中定义参数的四种方式

Python中函数参数的定义主要有四种方式: 1. F(arg1,arg2,…) 这是最常见的定义方式,一个函数可以定义任意个参数,每个参数间用逗号分割,用这种方式定义的函数在调用的的时...