python——ppt文件转成pdf文件

一、ppt文件转成pdf文件

需要的库pypiwin32
安装命令: pip install pypiwin32

代码为:

python 复制代码
import os
import glob
from win32com.client import gencache
def GetFilePath():
    #obtain the path of all ppt/pptx files in this directory
    # filePath=os.path.split(os.path.abspath(__file__))[0] #这里是获取当前文件路径下的所有ppt文件
    filePath='D:\\11' # 需要把PPT文件放到D盘的11文件夹里
    pptFiles=glob.glob(os.path.join(filePath,"*.ppt*"))
    #print(pptFiles)
    return filePath,pptFiles 

def pptToPDF(filename,new_directory):
    #Example Initialize a PowerPoint application instance
    pdfName=os.path.basename(filename).split('.ppt')[0]+'.pdf'
    savePathFile=os.path.join(new_directory,pdfName)
    if os.path.isfile(savePathFile): #judge if the str savePathFile is a file existed
        print(pdfName,"converted already")
        return
    p=gencache.EnsureDispatch("PowerPoint.Application")# guessing that has some connections with open the PowerPoint Application
    try:
        ppt=p.Presentations.Open(filename,False,False,False)#open the PowerPoint file 
    except Exception as e:
        print(os.path.split(filename)[1],"File format conversion failed,because %s" %e)  #throw 
    ppt.ExportAsFixedFormat(savePathFile,2,PrintRange=None)
    print("converted && saved :", savePathFile)
    p.Quit     #close the PowerPoint file       

def main():
    filePath,pptFlies=GetFilePath();#PPT文件所在目录和所有ppt文件目录的列表
    NewDirectory=os.path.join(filePath,"converted_directory")
    #生成PDF文件的存储路径
    if not os.path.exists(NewDirectory):
        os.mkdir(os.path.join(NewDirectory))#crate the directory if it not exits
    for each in pptFlies:
        pptToPDF(each,NewDirectory)

if __name__ =="__main__":
    main()  
    print("程序已经执行完成!")
    inp = input("请按回车键退出程序。")

把ppt文件放到D盘的11文件夹里,然后运行程序即可

打包好的exe文件可以到这里下载:https://download.csdn.net/download/wy313622821/89602307

二、pdf文件转成ppt文件

python 复制代码
# 需要导入的库:pip install pdf2pptx     pip install PyPDF2
import pdf2pptx
import PyPDF2

# 获取pdf文档有多少页
def get_pdf_page_count(pdf_path):
    with open(pdf_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        return len(reader.pages)

def convert_pdf_to_pptx(pdf_file_path, pptx_file_path):
    page_count = get_pdf_page_count(pdf_file_path)
    pdf2pptx.convert_pdf2pptx(pdf_file_path,pptx_file_path,300,0,page_count)

if __name__ =="__main__":
    # 指定输入的PDF文件路径和输出的PPT文件路径
    pdf_file = "D:\\11\\22.pdf"
    pptx_file = "D:\\11\\22.pptx"

    # 调用函数,传入PDF文件路径和PPT文件路径
    convert_pdf_to_pptx(pdf_file, pptx_file)  

    
    print("程序已经执行完成!")
    inp = input("请按回车键退出程序。")

打包好的exe文件可以到这里下载:

相关推荐
_OP_CHEN7 分钟前
C++进阶:(九)深度剖析unordered_map 与 unordered_set容器
开发语言·c++·stl容器·哈希表·哈希桶·unordered_map·unordered_set
七夜zippoe9 分钟前
Java并发编程基石:深入理解JMM(Java内存模型)与Happens-Before规则
java·开发语言·spring·jmm·happens-before
Mark Studio20 分钟前
QT linux 静态编译问题记录
开发语言·qt
爱打球的白师傅43 分钟前
python机器学习工程化demo(包含训练模型,预测数据,模型列表,模型详情,删除模型)支持线性回归、逻辑回归、决策树、SVC、随机森林等模型
人工智能·python·深度学习·机器学习·flask·逻辑回归·线性回归
造价女工1 小时前
PDF转图片:轻松实现工程图纸的高效共享与高清展示
pdf·格式工厂
无敌最俊朗@1 小时前
C++-Qt-音视频-基础问题01
开发语言·c++
kyle~1 小时前
C++---万能指针 void* (不绑定具体数据类型,能指向任意类型的内存地址)
开发语言·c++
MediaTea1 小时前
Python 第三方库:TensorFlow(深度学习框架)
开发语言·人工智能·python·深度学习·tensorflow
vortex51 小时前
Bash Glob 通配符详细指南:从 POSIX 标准到高级用法
开发语言·bash
KdanMin1 小时前
Android MediaCodec 硬编解码实战:从Camera预览到H264流与回环渲染
android·开发语言