Skip to content

Commit e5cfccc

Browse files
authored
Merge pull request #822 from cavilosa/main
PPT to Video convertor
2 parents bf60b09 + 21c480a commit e5cfccc

File tree

4 files changed

+143
-0
lines changed

4 files changed

+143
-0
lines changed

ppt2video/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
env
2+
test_video.mp4
3+
test.pptx

ppt2video/README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# PPT TO VIDEO CONVERTOR
2+
This automated script can be used on Windows to convert Power Point
3+
Presentation slides into mp4 video format using win32com.client.
4+
A user can add custom text slides before each presentation slide to be included in the video.
5+
For other systems it would require to use Python2.7 version to be able to install pypiwin32/pywin32 cause of the syntax incompatibility with Python3.
6+
7+
## Virtual environment
8+
9+
You can create <a href="https://packaging.python.org/en/latest/guides/installing-using-pip-and-virtual-environments/">virtual environment</a> to install the dependencies in a separate environment not to interfere with other packages on your machine.
10+
11+
## To install dependencies
12+
```
13+
pip install -r requirements.txt
14+
```
15+
16+
## To run the script
17+
Fill in:<br>
18+
**file_name <br>
19+
video_name<br>
20+
UseTimingsAndNarrations<br>
21+
DefaultSlideDuration<br>
22+
VertResolution<br>
23+
FramesPerSecond<br>
24+
Quality variables<br>
25+
input_dict**
26+
27+
with desired values in convertor.py, more instructions are in the script, and run it:
28+
```
29+
python3 convertor.py
30+
```
31+
32+
## Adding slides
33+
A user can add text slides before the desired slides by adding the slide number and the text as
34+
keys and values to the input dictionary or leave it blank.

ppt2video/convertor.py

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

ppt2video/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pypiwin32==223
2+
pywin32==303

0 commit comments

Comments
 (0)