ppt2mp4(Python2.7)将ppt文件转成mp4视频。GitHub先决条件1.需要Windows系统,并启用Windows多媒体播放器。需要安装以前版本的Office2010。因为PowerPoint需要依赖WindowsMultimediaPlayer将ppt转为视频,如果没有开启,需要从设置中开启桌面体验功能。启用步骤可参考:https://www.omnivex.com/suppo...使用1.安装Office2010或更高版本2.启用桌面体验功能(如果未启用)。3、编辑convert.py,修改ppt_path、mp4_path路径,然后运行pythonconvert.py。#-*-编码:UTF-8-*-importwin32com.clientimporttimeimportosimportshutildefppt_to_mp4(ppt_path,mp4_target,resolution=720,frames=24,quality=60,timeout=120):#status:转换结果。0:失败。-1:超时。1:成功。status=0ifppt_path==''ormp4_target=='':returnstatus#start_tm:开始时间start_tm=time.time()#创建一个不存在的文件夹。sdir=mp4_target[:mp4_target.rfind('\\')]ifnotos.path.exists(sdir):os.makedirs(sdir)#开始转换ppt=win32com.client.Dispatch('PowerPoint.Application')presentation=ppt.Presentations.Open(ppt_path,WithWindow=False)#CreateVideo()函数用法:https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.createvideopresentation.CreateVideo(mp4_target,-1,1,resolution,frames,quality)whileTrue:try:time.sleep(0.1)iftime.time()-start_tm>timeout:#转换超时。终止PowerPoint进程(将抛出异常)。os.system("taskkill/f/imPOWERPNT.EXE")status=-1breakifos.path.exists(mp4_path)andos.path.getsize(mp4_target)==0:#转换时文件大小为0字节不完成。continuestatus=1breakexceptException,e:print'错误!代码:{c},Message,{m}'.format(c=type(e).__name__,m=str(e))breakprinttime.time()-start_tmifstatus!=-1:ppt.Quit()returnstatusif__name__=='__main__':#RequireWindowssystem(MediaPlayerwasenabled)andMicrosoftOffice2010orhigher.#ppt转视频依赖WindowsMediaPlayer。所以你需要启用桌面体验功能。#更多保存类型请访问:https://docs.microsoft.com/en-us/office/vba/api/powerpoint.pp另存为文件类型#质量:0-100。幻灯片的质量级别。数字越大,质量越高。quality=60#resolution:幻灯片的分辨率。480,720,1080...resolution=720#frames:每秒的帧数。frames=24#ppt_path:ppt/pptx/pptm文件路径。ppt_path=os.path.abspath('./test.pptx')#mp4_path:mp4视频保存路径。mp4_path=os.path.abspath('./test.mp4')#ie_temp_dir:转换缓存文件路径。#默认路径(隐藏)是“C:/Users/username/AppData/Local/Microsoft/Windows/TemporaryInternetFiles/Content.MSO/ppt”。#或'C:/Users/username/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/ppt'#您可以在IE设置中找到缓存文件夹。#如果您不想清除缓存文件,请将ie_temp_dir分配为空字符串。#ie_temp_dir='C:/Users/username/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/ppt'ie_temp_dir=''#status:转换结果。0:失败。-1:超时。1:成功。status=0#timeout:转换超时的秒数。timeout=4*60try:status=ppt_to_mp4(ppt_path,mp4_path,resolution,frames,quality,timeout)#转换完成后清除PowerPoint缓存。当您转换数百个文件时,缓存文件夹将非常庞大。如果ie_temp_dir!='':shutil.rmtree(ie_temp_dir,ignore_errors=True)除了异常,e:print'Error!代码:{c},消息,{m}'.format(c=type(e).__name__,m=str(e))ifstatus==-1:print'Failed:timeout.'elif状态==1:打印“成功!”else:ifos.path.exists(mp4_path):os.remove(mp4_path)print'失败:ppt可能包含未知元素。您可以尝试将其转换为手动。
