Last active
May 5, 2023 03:48
-
-
Save 0187773933/d427c1f72d2caac82549a7d217e1084c to your computer and use it in GitHub Desktop.
Converts Images in Directory to PowerPoint Slides
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import glob | |
import collections # cause of yew python 3.10 | |
import collections.abc # cause of yew python 3.10 | |
# import pptx # pip install python-pptx | |
from pptx import Presentation | |
from pptx.util import Inches | |
from pptx.dml.color import RGBColor | |
from PIL import Image | |
def px_to_emu(pixels): | |
return int(pixels * 9525) | |
def add_image_slide(presentation, image_path): | |
slide_layout = presentation.slide_layouts[6] # Use a blank slide layout without title | |
slide = presentation.slides.add_slide(slide_layout) | |
# Set the slide background to black | |
background = slide.background | |
fill = background.fill | |
fill.solid() | |
fill.fore_color.rgb = RGBColor(0, 0, 0) | |
# Center and maintain aspect ratio | |
image = Image.open(image_path) | |
image_width, image_height = image.size | |
aspect_ratio = float(image_width) / float(image_height) | |
slide_width = presentation.slide_width | |
slide_height = presentation.slide_height | |
# Convert the slide dimensions from EMU to pixels | |
slide_width_px = int(slide_width / 9525) | |
slide_height_px = int(slide_height / 9525) | |
new_width = image_width | |
new_height = image_height | |
if (image_width > slide_width_px) or (image_height > slide_height_px): | |
if aspect_ratio > 1: | |
max_height = int(slide_width_px / aspect_ratio) | |
if max_height > slide_height_px: | |
new_height = slide_height_px | |
else: | |
new_height = max_height | |
new_width = int(new_height * aspect_ratio) | |
# print( "Landscape Mode" ) | |
# print( image_width , slide_width_px , new_width ) | |
# print( image_height , slide_height_px , new_height ) | |
else: | |
max_width = int(slide_height_px * aspect_ratio) | |
if max_width > slide_width_px: | |
new_width = slide_width_px | |
else: | |
new_width = max_width | |
new_height = int(new_width / aspect_ratio) | |
# print( "Portrait Mode" ) | |
# print( image_width , slide_width_px , new_width ) | |
# print( image_height , slide_height_px , new_height ) | |
left = int((slide_width - px_to_emu(new_width)) / 2) | |
top = int((slide_height - px_to_emu(new_height)) / 2) | |
slide.shapes.add_picture(image_path, left, top, width=px_to_emu(new_width), height=px_to_emu(new_height)) | |
if __name__ == "__main__": | |
# Path to directory with images | |
image_dir = "/path/to/images" | |
output_ppt = 'output.pptx' | |
# Create a new PowerPoint presentation | |
presentation = Presentation() | |
presentation.slide_width = Inches(13.33) | |
presentation.slide_height = Inches(7.5) | |
# Iterate through all image files in the directory | |
for image_path in glob.glob(os.path.join(image_dir, '*.*')): | |
if image_path.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp', '.tiff')): | |
add_image_slide(presentation, image_path) | |
# Save the presentation | |
presentation.save(output_ppt) | |
print(f"Presentation created successfully as '{output_ppt}'.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment