Untitled

mail@pastecode.io avatar
unknown
plain_text
8 months ago
1.3 kB
2
Indexable
Never
from PIL import Image
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
import os
import tkinter as tk
from tkinter import filedialog

def create_pdf(png_files, output_folder):
    if not os.path.exists(output_folder):
        os.makedirs(output_folder)

    unique_name = os.path.splitext(png_files[0])[0].split('(')[0]
    output_pdf = os.path.join(output_folder, f"{unique_name}.pdf")

    c = canvas.Canvas(output_pdf, pagesize=letter)

    for png_file in png_files:
        img = Image.open(os.path.join(input_folder, png_file))
        width, height = img.size

        if width > height:
            img = img.transpose(Image.ROTATE_90)

        c.drawImage(img, 0, 0, width=letter[0], height=letter[1])
        c.showPage()

    c.save()

root = tk.Tk()
root.withdraw()
input_folder = filedialog.askdirectory(title="Select the folder containing PNG files")

if not input_folder:
    print("Input folder selection cancelled. Exiting.")
else:
    png_files = [f for f in os.listdir(input_folder) if f.endswith('.png')]
    unique_names = set(os.path.splitext(file)[0].split('(')[0] for file in png_files)

    for unique_name in unique_names:
        group_files = [file for file in png_files if file.startswith(unique_name)]
        create_pdf(group_files, input_folder)