This simple script makes something that could be useful. You got some png files and you want to include them into a pdf file to make a presentation, an ebook or something like this. What if you can do it with a simple python script? Yes, it’s easy, you know that with python you can do almost everything with few lines of code if you import the right modules. So let’s see this code, I use it often, cause it’s handy to unite stuff about an argument in one place and to make tutorials and stuff like that in seconds.
from PIL import Image from glob import glob import os files = glob("*.png") iml = [] print(f"{files=}") files = sorted(files) for img in files: imgs = Image.open(img) rgb_im = imgs.convert('RGB') # to prevent errors: cannot save mode RGBA iml.append(rgb_im) pdf = "ALL.pdf" print(iml) image = iml[0] iml.pop(0) image.save(pdf, "PDF" , resolution=100.0, save_all=True, append_images=iml) os.system(pdf)
So, you got something you want to save on your pc? On windows press shift + windows button + s and get a part of the screen, save different images and then run this script in the folder where you saved the images. In a second you will have a pdf with all the images into it.
Making a simple GUI
Ok, now let’s make a basic gui for this script
from PIL import Image from glob import glob import os import tkinter as tk def create_pdf(name_pdf): files = glob("*.png") iml = [] print(f"{files=}") files = sorted(files) for img in files: imgs = Image.open(img) rgb_im = imgs.convert('RGB') # to prevent errors: cannot save mode RGBA iml.append(rgb_im) pdf = name_pdf + ".pdf" print(iml) image = iml[0] iml.pop(0) image.save(pdf, "PDF" , resolution=100.0, save_all=True, append_images=iml) os.system(pdf) root = tk.Tk() text = tk.Label(root, text="Nome del file:").pack() namepdf = tk.StringVar() text = tk.Entry(root, textvariable=namepdf) text.pack() button = tk.Button(root, text="Create pdf", command=lambda: create_pdf(namepdf.get()) ) button.pack() root.mainloop()
Subscribe to the newsletter for updates
Tkinter templates
My youtube channel
Twitter: @pythonprogrammi - python_pygame