Created
May 20, 2025 08:25
-
-
Save dutta-alankar/553986d76142fe93267acf8baad4ea09 to your computer and use it in GitHub Desktop.
Create pptx from images
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 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| Created on Tue May 20 10:18:24 2025 | |
| @author: alankar | |
| install module python-pptx | |
| """ | |
| from pptx import Presentation | |
| from pptx.util import Inches, Pt | |
| from PIL import Image | |
| import os | |
| # Folder containing PNG images | |
| image_folder = "/Users/alankard/Downloads/output-c100,m1.496,T4e4,t0.20,r70.671/temperature" | |
| output_pptx = f"{image_folder}/slides_from_pngs.pptx" | |
| # Get sorted list of PNG files | |
| image_files = sorted([f for f in os.listdir(image_folder) if f.lower().endswith(".png")]) | |
| # Open the first image to get dimensions | |
| first_image_path = os.path.join(image_folder, image_files[0]) | |
| with Image.open(first_image_path) as img: | |
| width_px, height_px = img.size | |
| dpi = img.info.get("dpi", (96, 96))[0] # Default to 96 if not specified | |
| width_in = width_px / dpi | |
| height_in = height_px / dpi | |
| # Convert inches to EMU (English Metric Units) used by pptx | |
| from pptx.util import Inches | |
| slide_width = Inches(width_in) | |
| slide_height = Inches(height_in) | |
| # Create a presentation with custom dimensions | |
| prs = Presentation() | |
| prs.slide_width = slide_width | |
| prs.slide_height = slide_height | |
| # Add each image as a full-slide image | |
| for image_file in image_files: | |
| img_path = os.path.join(image_folder, image_file) | |
| slide = prs.slides.add_slide(prs.slide_layouts[6]) # blank slide | |
| slide.shapes.add_picture(img_path, 0, 0, width=slide_width, height=slide_height) | |
| # Save the presentation | |
| prs.save(output_pptx) | |
| print(f"Presentation saved as {output_pptx}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment