convert.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #!/usr/bin/env python
  2. # -*- encoding: utf-8 -*-
  3. '''
  4. @Author : liuyuqi
  5. @Contact : liuyuqi.gov@msn.cn
  6. @Time : 2019/12/16 22:20:01
  7. @Version : 1.0
  8. @License : (C)Copyright 2019
  9. @Desc : ppt 文件转换为 mp4,利用powerpoint自动转换功能。
  10. '''
  11. import win32com.client
  12. import time
  13. import os
  14. import shutil
  15. def ppt_to_mp4(ppt_path,mp4_target,resolution = 720,frames = 24,quality = 60,timeout = 120):
  16. # status:Convert result. 0:failed. -1: timeout. 1:success.
  17. status = 0
  18. if ppt_path == '' or mp4_target == '':
  19. return status
  20. # start_tm:Start time
  21. start_tm = time.time()
  22. # Create a folder that does not exist.
  23. sdir = mp4_target[:mp4_target.rfind('\\')]
  24. if not os.path.exists(sdir):
  25. os.makedirs(sdir)
  26. # Start converting
  27. ppt = win32com.client.Dispatch('PowerPoint.Application')
  28. presentation = ppt.Presentations.Open(ppt_path,WithWindow=False)
  29. # CreateVideo() function usage: https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.createvideo
  30. presentation.CreateVideo(mp4_target,-1,1,resolution,frames,quality)
  31. while True:
  32. try:
  33. time.sleep(0.1)
  34. if time.time() - start_tm > timeout:
  35. # Converting time out. Killing the PowerPoint process(An exception will be threw out).
  36. os.system("taskkill /f /im POWERPNT.EXE")
  37. status = -1
  38. break
  39. if os.path.exists(mp4_path) and os.path.getsize(mp4_target) == 0:
  40. # The filesize is 0 bytes when convert do not complete.
  41. continue
  42. status = 1
  43. break
  44. except Exception, e:
  45. print('Error! Code: {c}, Message, {m}'.format(c = type(e).__name__, m = str(e)))
  46. break
  47. print(time.time()-start_tm)
  48. if status != -1:
  49. ppt.Quit()
  50. return status
  51. if __name__ == '__main__':
  52. # Require Windows system(Media Player was enabled) and Microsoft Office 2010 or higher.
  53. # Converting ppt into video relies on Windows Media Player. So you need to enable Desktop Experience feature.
  54. # More save types please visit: https://docs.microsoft.com/en-us/office/vba/api/powerpoint.ppsaveasfiletype
  55. # quality:0-100. The level of quality of the slide. The higher the number, the higher the quality.
  56. quality = 60
  57. # resolution:The resolution of the slide. 480,720,1080...
  58. resolution = 720
  59. # frames: The number of frames per second.
  60. frames = 24
  61. # ppt_path:The ppt/pptx/pptm file path.
  62. ppt_path = os.path.abspath('./test.pptx')
  63. # mp4_path:The mp4 video save path.
  64. mp4_path = os.path.abspath('./test.mp4')
  65. # ie_temp_dir:The convert cache file path.
  66. # The default path (hidden) is 'C:/Users/username/AppData/Local/Microsoft/Windows/Temporary Internet Files/Content.MSO/ppt'.
  67. # Or 'C:/Users/username/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/ppt'
  68. # You can find the cache folde at IE setting.
  69. # If you don't want clear cache files,assign ie_temp_dir with empty string.
  70. #ie_temp_dir = 'C:/Users/username/AppData/Local/Microsoft/Windows/INetCache/Content.MSO/ppt'
  71. ie_temp_dir = ''
  72. # status:Converting result. 0:failed. -1: timeout. 1:success.
  73. status = 0
  74. # timeout: Seconds that converting time out.
  75. timeout = 4*60
  76. try:
  77. status = ppt_to_mp4(ppt_path,mp4_path,resolution,frames,quality,timeout)
  78. # Clear PowerPoint cache after convert completed. When you converted hundreds of files, the cache folder will be huge.
  79. if ie_temp_dir != '':
  80. shutil.rmtree(ie_temp_dir, ignore_errors=True)
  81. except Exception, e:
  82. print('Error! Code: {c}, Message, {m}'.format(c = type(e).__name__, m = str(e)))
  83. if status == -1:
  84. print('Failed:timeout.')
  85. elif status == 1:
  86. print('Success!')
  87. else:
  88. if os.path.exists(mp4_path):
  89. os.remove(mp4_path)
  90. print('Failed:The ppt may have unknow elements. You can try to convert it manual.')