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文件可以到这里下载:

相关推荐
兮动人13 分钟前
Java 单元测试中的 Mockito 使用详解与实战指南
java·开发语言·单元测试
武子康21 分钟前
Java-151 深入浅出 MongoDB 索引详解 性能优化:慢查询分析 索引调优 快速定位并解决慢查询
java·开发语言·数据库·sql·mongodb·性能优化·nosql
蓝桉~MLGT27 分钟前
Python学习历程——字符串相关操作及正则表达式
python·学习·正则表达式
一晌小贪欢28 分钟前
Python爬虫第5课:正则表达式与数据清洗技术
爬虫·python·正则表达式·网络爬虫·python爬虫·python3·网页爬虫
Nina_7171 小时前
Google提示词白皮书总结(2)
人工智能·python
Lynnxiaowen1 小时前
今天我们继续学习python3编程之python基础
linux·运维·python·学习
hui函数1 小时前
Python全栈(基础篇)——Day10:后端内容(map+reduce+filter+sorted+实战演示+每日一题)
后端·python
励志成为美貌才华为一体的女子2 小时前
本地用docling实现pdf转markdown操作笔记
笔记·pdf
患得患失9492 小时前
【Turborepo】【Next】 Turborepo + Next.js 前后端精简搭建笔记(带官网)
开发语言·javascript·笔记
拓端研究室2 小时前
视频讲解|Python遗传算法GA在车辆路径规划VRP数据优化中的应用
开发语言·人工智能·r语言