Png joined in one PDF files

Whith this script you can put all the images in one pdf file.

What do you need to install with pip

Install fpdf and pillow

pip install fpdf

pip install pillow

File flowchart

  • take a list of all png in the directory (os.listdir)
  • get width and height of the greatest image
  • input the filename
  • create and istance of FPDF
  • use add_page and image for the istance to add pages and images to pages
  • save file with output
  • show file with os.startfile
from fpdf import FPDF
from PIL import Image
import os


def makePdf():
    "Takes filename and number of pages abd creates pdf"
    lst_files = [
        f for f in os.listdir()
        if f.endswith(".png") or f.endswith(".PNG")]
    w = 800
    h = 600
    for f in lst_files:
        cover = Image.open(f)
        if cover.size[0] > w:
            w = cover.size[0]
        if cover.size[1] > h:
            h = cover.size[1]
    nomefile = input("Nomedelfile: ")
    pdf = FPDF(unit="pt", format=[w, h])
    for page in lst_files:
        pdf.add_page()
        pdf.image(page, 0, 0)
    pdf.output(nomefile + ".pdf", "F")
    os.startfile(nomefile + ".pdf")

makePdf()

In this post you get another application of this code, click here to see it.


Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.