GitHub#python#:ppt(批量)转换为pdf

本文介绍如何使用Python脚本实现单个及批量PPT文件到PDF的转换过程,并提供详细的代码示例。该方法适用于Windows环境,利用win32com与comtypes库操作PowerPoint应用程序。

1.单个文件转化

网址:https://github.com/Ryuodan/PPTtoPDF-Converter

支持python3,给出代码:

import win32com.client
import os
in_file=input("Enter the path of the file")
out_file=os.path.splitext(in_file)[0]     # desktop\file.pptx 
powerpoint=win32com.client.Dispatch("Powerpoint.Application")
pdf=powerpoint.Presentations.Open(in_file,WithWindow=False)
pdf.SaveAs(out_file,32)
pdf.Close()
powerpoint.Quit()

运行:

D:\test>python PPTtoPDF.py
Enter the path of the fileD:\test\pic.pptx

过程中会显示:


完成后:



2.批量文件转化

源码网址:https://github.com/jackbrookes/batch-ppt-to-pdf/blob/master/batch_ppt_to_pdf.py

文章网址:http://mp.weixin.qq.com/s/lPL_HaL3acflmp-Yz6OSIQ

依赖的库:comtypes,windows下的安装:

D:\test>python -m pip install moviepy

源代码:

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((".pptx", ".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()

运行:

D:\test>python main.py

运行结束后,会将D:\test文件夹里以pptx为结尾的ppt全部转化为pdf格式。

运行前:


运行后:


3.其他的转化

office to pdf:https://github.com/mlkyo/office2pdf

ppt to pdf or picture:https://github.com/vijayanandrp/PDF-to-PPT

ppt to pdf:https://github.com/GijsTimmers/ppt2pdf

ppt or pdf to HTML or json:https://github.com/lucaslukero/PDF-PPT-ODP-to-HTML-JSON-for-Tidys

pdf to ppt:https://github.com/uesseu/pdf2pptx

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值