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

前言

距离运营人员提出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 
复制代码

结尾

感谢jym的阅读

公众号:微信搜【进阶的大前端】

猜你喜欢

转载自juejin.im/post/7042203010390786078
今日推荐