Python实现PPT批量转PDF:仅需27行代码的自动化方案

不晚
不晚
发布时间:2026-10-13 | 2人浏览过
原创

本文介绍一个使用Python编写的轻量级脚本,仅需27行代码即可实现将指定文件夹内的所有.ppt或.pptx文件批量转换为PDF格式。通过调用Windows COM接口,该脚本自动遍历当前目录下的演示文稿文件并执行转换操作,适合需要高效处理大量PPT文档的用户。

该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 相关产品和应用

全部下载

专题列表