app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# Python3 program to convert all images in a directory to a single PDF # using img2pdf library import img2pdf import os # Directory containing images img_dir = "images" # Change this to your directory path pdf_path = "output.pdf" # Output PDF file name # Supported image extensions valid_exts = (".jpg", ".jpeg", ".png", ".bmp", ".tiff") # Collect image paths from the directory img_files = [os.path.join(img_dir, f) for f in sorted(os.listdir(img_dir)) if f.lower().endswith(valid_exts)] # Ensure there are images if not img_files: print("No valid image files found in the directory.") else: # Convert to PDF with open(pdf_path, "wb") as f: f.write(img2pdf.convert(img_files)) print(f"Successfully created PDF: {pdf_path}") |