Fix Python – Create PDF from a list of images

Question

Asked By – macabeus

Is there any practical way to create a PDF from a list of images files, using Python?

In Perl I know that module. With it I can create a PDF in just 3 lines:

use PDF::FromImage;
...
my $pdf = PDF::FromImage->new;
$pdf->load_images(@allPagesDir);
$pdf->write_file($bookName . '.pdf');

I need to do something very similar to this, but in Python. I know the pyPdf module, but I would like something simple.

Now we will see solution for issue: Create PDF from a list of images


Answer

Install FPDF for Python:

pip install fpdf

Now you can use the same logic:

from fpdf import FPDF
pdf = FPDF()
# imagelist is the list with all image filenames
for image in imagelist:
    pdf.add_page()
    pdf.image(image,x,y,w,h)
pdf.output("yourfile.pdf", "F")

You can find more info at the tutorial page or the official documentation.

This question is answered By – Ilya Vinnichenko

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0