python PPT批量制作封面图及导出视频教程

本文介绍了如何使用Python在Windows环境下自动化将PPT转换为图片和视频,并进一步拼接生成封面图。通过读取目录下的PPT,利用Microsoft Office的接口实现转换,最后展示了一个完整的脚本流程。此方法显著提高了工作效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

距离运营人员提出[PPT,word自动转图片]没多久后。有天运营跑过来说道,能不能自动生成封面图,以及视频。因为每次手动一个一个打开PPT再用islide导出封面图工作很繁琐,效率也不高。
有了上次写PPT,word自动转图片的栗子,这次写起来也十分顺手了。

本文python版本3.9.5
需windows 平台,需安装Microsoft Office

脚本思路

python读取目录下的PPT->打开PPT->导出图片->拿到图片拼接制作封面图。
先来看看运营要求的封面图,读取PPT的前7张,第一张宽度铺满,第二张开始等比分配

拿到PPT集合
def getPpt():
     files = os.listdir("./"+pptFilePath)
     return files
删除文件夹
def removeFileInFirstDir(targetDir):
    for file in os.listdir(targetDir): 
        targetFile = os.path.join(targetDir,  file) 
        if os.path.isfile(targetFile): 
            os.remove(targetFile)
PPT转图片顺便把视频也转出来
def ppt2png(ppt_path,powerpoint,fileName):
    try:
        ppt = powerpoint.Presentations.Open(ppt_path)
        #保存为图片
        img_path = os.path.abspath(imgFilePath + '.png')
        ppt.SaveAs(img_path, 18) # 17保存为jpg格式
         # quality:0-100. The level of quality of the slide. The higher the number, the higher the quality.
        quality = 60
        # resolution:The resolution of the slide. 480,720,1080...
        resolution = 720
        # frames: The number of frames per second.
        frames = 24
        mp4_target = os.path.abspath('./ppt/'+fileName+'.mp4')
        print(mp4_target)
        print(fileName,"----正在转成视频")
        ppt.CreateVideo(mp4_target,-1,1,resolution,frames,quality)
        while True:
            try:
                time.sleep(0.1)
                if os.path.exists(mp4_target) and os.path.getsize(mp4_target) == 0:
                    # The filesize is 0 bytes when convert do not complete.
                    continue
                break
            except Exception as e:
                print (e)
                break
        # 关闭打开的ppt文件
        ppt.Close()
    except IOError:
        print('PPT转png失败',ppt_path)
    else:
        print('\n')
        print('\n')
        print("PPT转png成功",ppt_path)
图片拼接成封面图
def getCover(fileName):
    # 封面图  w 1012 h 1431 空白6 
    w = 1012
    h = 1431
    padding = 6
    toImage = Image.new('RGB',(w,h),"#E6F5FF") # #E6F5FF填充背景
    files = os.listdir("./"+imgFilePath)
    files.sort(key = lambda x : int(x.split('.')[0][3:])) #使用sort进行按顺序读取
    for index,value in enumerate(files):
        if(index <7 ):
            _path = os.path.abspath('./img/'+value)
            pic_fole_head = Image.open(_path)
            # 获取图片的尺寸
            if(index==0):
                #第一张图片宽度铺满 1012 - 6 -6 ,高度561 
                # 按照指定的尺寸,给图片重新赋值,<PIL.Image.Image image mode=RGB size=200x200 at 0x127B7978>
                tmppic = pic_fole_head.resize((w - padding * 2, 561))
                # 计算每个图片的左上角的坐标点(0, 0)
                loc = (6,6)
                toImage.paste(tmppic, loc)
            else:
                # 按照指定的尺寸,给图片重新赋值,<PIL.Image.Image image mode=RGB size=200x200 at 0x127B7978>
                # w = 1012
                # h = 1431
                # padding = 6
                smallW  = int((1012 - padding * 3 ) / 2 )
                tmppic = pic_fole_head.resize((smallW, 280))
                # 计算每个图片的左上角的坐标点(0, 0),(200, 0),(200, 200)
                line = math.ceil(index/2) #计算处于第几行
                x = 6 
                if(index % 2 ==0):
                    x = smallW + padding * 2
                loc = (int(x ), int((line-1) * 280 + 561 + (line+1)* 6))
                toImage.paste(tmppic, loc)
    toImage.save("./ppt/"+fileName+'.png')
    print(fileName+'封面生成成功')
初始化PPT
def init_powerpoint():
    powerpoint = win32com.client.Dispatch('PowerPoint.Application') #comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible = 1
    return powerpoint
最后调用
if __name__=='__main__':
    powerpoint = init_powerpoint()
    pptArr = getPpt()
    print('---pptArr--',pptArr)
    for index,value in enumerate(pptArr):
        if(('.ppt' in value) ==True):
            removeFileInFirstDir('./'+imgFilePath)
            _path = os.path.abspath('./ppt/'+value)
            fileName = os.path.basename(_path).split('.')[0]
            ppt2png(_path,powerpoint,fileName)
            getCover(fileName)
        time.sleep(2)
    powerpoint.Quit()
    # removeFileInFirstDir('./'+imgFilePath)
    input("输入任意键结束")

运行后的封面图及视频

最后打包成exe文件给运营人员用就可以了
pyinstaller -c -F -i a.ico index.py 
结尾

公众号
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值