-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertor.py
104 lines (89 loc) · 3.81 KB
/
convertor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import win32com.client
import os
import sys
# PpSaveAsFileType enumeration (PowerPoint)
ppSaveAsMP4 = 39
# PpSlideLayout enumeration (PowerPoint)
ppLayoutText = 6
# msoShapeRectangle
msoShapeRectangle = 1
def ppt2video(pptx, video, timing, duration,
resolution, frames, quality, dict):
# Check if system requirements are met.
if sys.platform == "win32":
pass
else:
print("Sorry, this script can be run only on Windows.")
# Create files pathes for the ppt and video files.
ppt_path = os.path.abspath(pptx)
video_path = os.path.abspath(video)
# Presentation object
# https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation
ppt = win32com.client.Dispatch("PowerPoint.Application")
# Open presentation
presentation = ppt.Presentations.Open(ppt_path, WithWindow=False)
new_presentation = ppt.Presentations.Add(WithWindow=False)
# Compare input and slides by indexes, creating lists and comparing them
keys = [int(key) for key, value in dict.items()]
slides = [slide.SlideIndex for slide in presentation.Slides]
# List of indexes with user input texts.
list = [key for key in keys if key in slides]
# Slides indexes start with 1
for slide in presentation.Slides:
# Checking if user provided input
if slide.SlideIndex in list:
# Create new slide with new text
len_new_ppt = len(new_presentation.Slides)
new_slide = new_presentation.Slides.Add(len_new_ppt + 1,
ppLayoutText)
new_slide.Shapes.addShape(
msoShapeRectangle, 150, 150, 250, 250). \
TextFrame.TextRange.Text = dict.get(str(slide.SlideIndex))
# Copying slide from original presentation and adding it new one.
slide.Copy()
len_new_ppt = len(new_presentation.Slides)
new_presentation.Slides.Paste(len_new_ppt + 1)
else:
# Adding slide to new presentation without user provided text
slide.Copy()
len_new_ppt = len(new_presentation.Slides)
new_presentation.Slides.Paste(len_new_ppt + 1)
# Presentation.CreateVideo method (PowerPoint)
# https://docs.microsoft.com/en-us/office/vba/api/powerpoint.presentation.createvideo
new_presentation.CreateVideo(video_path, timing, duration,
resolution, frames, quality)
while True:
try:
# Update the video file, if already exists.
os.rename(video_path, video_path)
print(f'The video from {pptx} has been created.')
break
except Exception:
pass
new_presentation.Close()
presentation.Close()
ppt.Quit()
pass
if __name__ == '__main__':
# e.g.:test.pptx, file expected to be in the root folder
file_name = ""
# e.g.: test_video, will be created in the root folder
video_name = ""
UseTimingsAndNarrations = False # Boolean value
DefaultSlideDuration = 2 # Int
VertResolution = 720 # Int
FramesPerSecond = 24 # Int
Quality = 60 # Int
# User Input is in dictionary format. The keys in the dict represent the
# number of the slides in the presentation.
# Slides indexes, and input_dict as well, start with 1, "first slide", e.g.
# {"1":"input before slide number 1, the first slide",
# "2":"input before slide number 2, the second slide",
# "4":"input before slide number 4, the forth slide"}
# You can choose to what slides to add precending text and what slides
# to leave without additional input.
input_dict = {}
ppt2video(f"./{file_name}", f"./{video_name}.mp4",
UseTimingsAndNarrations,
DefaultSlideDuration, VertResolution,
FramesPerSecond, Quality, input_dict)