|
| 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) |
0 commit comments