Skip to content

Instantly share code, notes, and snippets.

@kiyoon
Last active November 13, 2023 05:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiyoon/3b44ec38f8690dcc13c9267421d9cf54 to your computer and use it in GitHub Desktop.
Save kiyoon/3b44ec38f8690dcc13c9267421d9cf54 to your computer and use it in GitHub Desktop.
"""
This will make pptx slides containing images.
It assumes your images are square shaped.
"""
from glob import glob
from pptx import Presentation
from pptx.dml.color import RGBColor
from pptx.util import Inches
def create_pptx(images, output_pptx):
presentation = Presentation()
presentation.slide_width = Inches(13.3333)
presentation.slide_height = Inches(7.5)
for image_path in images:
slide = presentation.slides.add_slide(
presentation.slide_layouts[6]
) # Use blank slide layout
# Insert image on the slide to the top
# slide.shapes.add_picture(image_path, left, top, width, height)
# Insert image on the slide slightly below the top
left_img = Inches(3.16)
top_img = Inches(0.5)
width_img = height_img = Inches(7)
slide.shapes.add_picture(image_path, left_img, top_img, width_img, height_img)
left_txt = Inches(0)
top_txt = Inches(0)
width_txt = presentation.slide_width
height_txt = Inches(0.5)
textbox = slide.shapes.add_textbox(left_txt, top_txt, width_txt, height_txt)
textbox.text = image_path
textbox.text_frame.paragraphs[0].font.color.rgb = RGBColor(255, 0, 0)
textbox.text_frame.paragraphs[0].alignment = 2 # 2 = Center alignment
# Save the presentation
presentation.save(output_pptx)
if __name__ == "__main__":
image_paths = sorted(glob("*.jpg"))
output_file = "output_presentation.pptx"
create_pptx(image_paths, output_file)
print(f"Presentation '{output_file}' created successfully.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment