分享

将ppt文件转成mp4视频的Python脚本

 小世界的野孩子 2020-02-29

ppt2mp4 (Python2.7)

将ppt文件转成mp4视频。GitHub

前提

  • 1.需要Windows系统,并且启用了Windows多媒体播放器。需要安装Office 2010已上版本。因为PowerPoint把ppt转成视频需要依赖Windows多媒体播放器,如果没启用的话需要从设置里启用桌面体验功能。启用步骤可以参考:https://www./suppo...

使用

  • 1.安装Office 2010或更高版本
  • 2.启用桌面体验功能(如果没启用的话)。
  • 3.编辑convert.py,修改ppt_path,mp4_path路径,然后运行python convert.py

# -*- coding: UTF-8 -*-
import win32com.client
import time
import os
import shutil

def ppt_to_mp4(ppt_path,mp4_target,resolution = 720,frames = 24,quality = 60,timeout = 120):
    # status:Convert result. 0:failed. -1: timeout. 1:success.
    status = 0
    if ppt_path == '' or mp4_target == '':
        return status
    # start_tm:Start time
    start_tm = time.time()

    # Create a folder that does not exist.
    sdir = mp4_target[:mp4_target.rfind('\\')]
    if not os.path.exists(sdir):
        os.makedirs(sdir)

    # Start converting
    ppt = win32com.client.Dispatch('PowerPoint.Application')
    presentation = ppt.Presentations.Open(ppt_path,WithWindow=False)
    # CreateVideo() function usage: https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.createvideo
    presentation.CreateVideo(mp4_target,-1,1,resolution,frames,quality)
    while True:
        try:
            time.sleep(0.1)
            if time.time() - start_tm > timeout:
                # Converting time out. Killing the PowerPoint process(An exception will be threw out).
                os.system("taskkill /f /im POWERPNT.EXE")
                status = -1
                break
            if os.path.exists(mp4_path) and os.path.getsize(mp4_target) == 0:
                # The filesize is 0 bytes when convert do not complete.
                continue
            status = 1
            break
        except Exception, e:
            print 'Error! Code: {c}, Message, {m}'.format(c = type(e).__name__, m = str(e))
            break
    print time.time()-start_tm
    if status != -1:
        ppt.Quit()

    return status
    
if __name__ == '__main__':

    # Require Windows system(Media Player was enabled) and Microsoft Office 2010 or higher.
    # Converting ppt into video relies on Windows Media Player. So you need to enable Desktop Experience feature.
    # More save types please visit: https://docs.microsoft.com/en-us/office/vba/api/powerpoint.ppsaveasfiletype

    # 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

    # ppt_path:The ppt/pptx/pptm file path.
    ppt_path = os.path.abspath('./test.pptx')
    # mp4_path:The mp4 video save path.
    mp4_path = os.path.abspath('./test.mp4')

    # ie_temp_dir:The convert cache file path.
    # The default path (hidden) is 'C:/Users/username/AppData/Local/Microsoft/Windows/Temporary Internet Files/Content.MSO/ppt'.
    # Or 'C:/Users/username/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/ppt'
    # You can find the cache folde at IE setting.
    # If you don't want clear cache files,assign ie_temp_dir with empty string.
    #ie_temp_dir = 'C:/Users/username/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/ppt'
    ie_temp_dir = ''

    # status:Converting result. 0:failed. -1: timeout. 1:success.
    status = 0
    # timeout: Seconds that converting time out.
    timeout = 4*60
    try:
        status = ppt_to_mp4(ppt_path,mp4_path,resolution,frames,quality,timeout)
        # Clear PowerPoint cache after convert completed. When you converted hundreds of files, the cache folder will be huge.
        if ie_temp_dir != '':
            shutil.rmtree(ie_temp_dir, ignore_errors=True)
    except Exception, e:
        print 'Error! Code: {c}, Message, {m}'.format(c = type(e).__name__, m = str(e))
        
    if status == -1:
        print 'Failed:timeout.'
    elif status == 1:
        print 'Success!'
    else:
        if os.path.exists(mp4_path):
            os.remove(mp4_path)
        print 'Failed:The ppt may have unknow elements. You can try to convert it manual.'
        

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多