使用Python实现批量PPT转图片,格式支持PNG、JPG,转后图片后批量重命名

一、脚本功能

PPT批量转换图片,格式支持png、jpg(手动脚本中改参数)

二、环境说明

使用Windows环境,必须安装【Microsoft PowerPoint】

三、使用说明

建议新建一个文件夹,将python脚本和待转换的PPT(文件名建议不包含中文),放到一个文件夹中

四、实现步骤

  1. 先创建和PPT文件同名的空文件夹
  2. 将PPT转换为图片,放到该文件夹中(图片默认名称是幻灯片1.PNG,幻灯片2.PNG。。。)
  3. 将转换后的图片批量重命名,转换后的名称是1.png、2.png。。。

五、代码

git:github.com/chisanhe/ppt_to_img.git

import os
import win32com.client

'''
   支持转换为PNG、JPG,搜索后替换即可
'''
#JPG是17
ppSaveAsJPG = 17
#PNG是18
ppSaveAsPNG = 18

'''将PPT另存为图片格式
  arguments:
      pptFullName: 要转换的ppt文件,
      pptName:转换后的存放JPG文件的目录
      imgType: 图片类型
'''
def pptToImg(pptFullName, pptName, imgType):
    # 启动PPT
    pptClient = win32com.client.Dispatch('PowerPoint.Application')
    # 设置为0表示后台运行,不显示,1则显示
    pptClient.Visible = 1
    # 打开PPT文件
    ppt = pptClient.Presentations.Open(pptFullName)
    # 另存为图片
    ppt.SaveAs(pptName, imgType)
    # 退出
    pptClient.Quit()

'''
    多文件夹多图片文件重命名
'''
def renameImg(currentDir):
    #当前目录下,只获取所有的文件夹
    folders = [dI for dI in os.listdir(currentDir) if os.path.isdir(os.path.join(currentDir, dI))]
    i = 0;
    for folder in folders:
        #打开单个文件夹,获取文件列表
        fileList = os.listdir(folder)

        #重命名文件,规则1、2、3,开发时读取文件利于遍历
        i = 0;
        #遍历单个文件
        for file in fileList:
            #为了避免修改其他文件,只判断等于PNG、JPG的图
            fileFix = os.path.splitext(file)[-1]
            if fileFix.lower() == '.png' or fileFix == '.jpg':
                i += 1
                #完整路径img文件名 + 后缀,F:\my\projects\python\ppt2img\01_single_img/1.png
                imgFullName = os.path.join(currentDir, folder + '/' +  file)
                #完整路径img文件名不含后缀 F:\my\projects\python\ppt2img\01_single_img
                imgName = os.path.join(currentDir, folder)
                #重命名
                os.rename(imgFullName, imgName + '/' + (str(i) + fileFix.lower()))


if __name__ == '__main__':
    print("PPT转图片开始")
    # #获取当前路径
    currentDir = os.sys.path[0]
    #获取当前文件列表
    currentDirAllFiles = os.listdir(currentDir)
    #获取当前目录下所有后缀是ppt、pptx的文件,返回值是生成器对象(可迭代)
    currentDirPptFiles = (fns for fns in currentDirAllFiles if fns.endswith(('.ppt', '.pptx')))
    # 当前目录下所有的PPT文件名,和上述区别在于有无后缀名,返回值是生成器对象(可迭代)
    currentDirPptNames = (os.path.splitext(fns)[0] for fns in currentDirAllFiles if fns.endswith(('.ppt', '.pptx')))
    #fullFileName是文件名称 + 后缀01_single_img.pptx,fileName是文件名称不含后缀01_single_img
    for fullFileName, fileName in zip(currentDirPptFiles, currentDirPptNames):
        #完整路径ppt文件名 + 后缀,F:\my\projects\python\ppt2img\01_single_img.pptx
        pptFullName = os.path.join(currentDir, fullFileName)
        #完整路径PPT文件名 F:\my\projects\python\ppt2img\01_single_img
        pptName = os.path.join(currentDir, fileName)
        #需要创建一个与PPT同名的文件夹,判断下,如果不存在则创建,已存在忽略
        if not os.path.exists(pptName):
            os.mkdir(pptName)
           #PPT转PNG
           pptToImg(pptFullName, pptName, ppSaveAsPNG)

           #PPT转JPEG
           # pptToImg(pptFullName, pptName, ppSaveAsJPG)

    print("PPT转图片完成")

    print("图片重命名开始")
    renameImg(currentDir)
    print("图片重命名完成,脚本执行结束")
本作品采用《CC 协议》,转载必须注明作者和本文链接
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!