Skip to content

Instantly share code, notes, and snippets.

@Sciroccogti
Created August 20, 2020 10:16
Show Gist options
  • Save Sciroccogti/7b2d8d022707b23839bc38a6946dbc25 to your computer and use it in GitHub Desktop.
Save Sciroccogti/7b2d8d022707b23839bc38a6946dbc25 to your computer and use it in GitHub Desktop.
turn ppt to mp4
import os
from time import strftime
import time
from win32com import client
from tqdm import *
# Counts the number of files in the directory that can be converted
def n_files(directory):
total = 0
for file in os.listdir(directory):
if (file.endswith('.ppt') or file.endswith('.pptx') or file.endswith('.PPTX')):
total += 1
return total
# Creates a new directory within current directory called videos
def createFolder(directory):
if not os.path.exists(directory + '\\videos'):
os.makedirs(directory + '\\videos')
if __name__ == "__main__":
print('\nPlease note that this will overwrite any existing MP4 files')
print('For best results, close Microsoft PowerPoint before proceeding')
input('Press enter to continue.')
directory = os.getcwd()
if n_files(directory) == 0:
print('There are no files to convert')
exit()
createFolder(directory)
print('Starting conversion... \n')
# Opens each file with Microsoft PowerPoint and saves as a MP4
try:
app = client.Dispatch('PowerPoint.Application')
# app.Visible = False
# word = client.DispatchEx('Word.Application')
for file in os.listdir(directory):
if (file.endswith('.ppt') or file.endswith('.pptx') or file.endswith('.PPTX')):
ending = ""
if file.endswith('.ppt'):
ending = '.ppt'
if file.endswith('.pptx'):
ending = '.pptx'
if file.endswith('.PPTX'):
ending = '.PPTX'
new_name = file.replace(ending, r".mp4")
in_file = os.path.abspath(directory + '\\' + file)
new_file = os.path.abspath(
directory + '\\videos' + '\\' + new_name)
ppt = app.Presentations.Open(in_file, WithWindow=False)
# doc = word.Documents.Open(in_file)
# print(new_name)
timeout = int(30 * os.path.getsize(in_file) / 1024 / 1024)
if timeout < 5400:
timeout = 5400
ppt.CreateVideo(new_file)
pbar = tqdm(total=timeout, desc=new_file)
start_tm = time.time()
while True:
try:
time.sleep(10)
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(new_file) and os.path.getsize(new_file) == 0:
# The filesize is 0 bytes when convert do not complete.
pbar.update(10)
continue
status = 1
break
except Exception as e:
print('Error! Code: {c}, Message, {m}'.format(c=type(e).__name__, m=str(e)))
break
# ppt.SaveAs(new_file, FileFormat = 39)
# doc.SaveAs(new_file,FileFormat = 17)
ppt.Close()
except Exception as err:
print("Error: {0}".format(err))
finally:
# word.Quit()
app.Quit()
print('\nConversion finished at ' + strftime("%H:%M:%S"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment