I am wondering if there is an easy way to combine multiple png images into a single pdf in python. I want each image to be a single page in the pdf. Is pypdf the best librar
There's a python port to WKHtmlToPdf
:
https://pypi.python.org/pypi/wkhtmltopdf/0.1
Easy to create page breaks between img
tags in an html doc using css which you can pass to this lib.
http://wkhtmltopdf.org/usage/wkhtmltopdf.txt
I've got the same problem.
So I've created python function to unite multiple pictures in one pdf. Code is available at my github page on https://github.com/wizard1989/Unite-multiple-pictures-into-pdf. It uses "reportlab".
Code is based on answers from the following links:
Create PDF from a list of images
Combining multiple pngs in a single pdf in python
png images to one pdf in python
How can I convert all JPG files in a folder to PDFs and combine them?
https://www.blog.pythonlibrary.org/2012/01/07/reportlab-converting-hundreds-of-images-into-pdfs/
Here is example of how to unite images into pdf.
We have folder "D:\pictures" with pictures of types png and jpg. We want to create file pdf_with_pictures.pdf out of them and save it in the same folder.
outputPdfName = "pdf_with_pictures"
pathToSavePdfTo = "D:\\pictures"
pathToPictures = "D:\\pictures"
splitType = "none"
numberOfEntitiesInOnePdf = 1
listWithImagesExtensions = ["png", "jpg"]
picturesAreInRootFolder = True
nameOfPart = "volume"
unite_pictures_into_pdf(outputPdfName, pathToSavePdfTo, pathToPictures, splitType, numberOfEntitiesInOnePdf, listWithImagesExtensions, picturesAreInRootFolder, nameOfPart)
I've recently found img2pdf, very useful and easy to use... the code is so simply as following (and you can complicate it with some others additional parameters):
img2pdf *png -o output_all_pngs.pdf
I hope it helps you!
from PIL import Image
image1 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi24.jpg')
image2 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/Gopi-1.jpg')
image3 = Image.open(r'C:/Users/uidg3972/Desktop/New folder/SNP_1291.jpg')
im1 = image1.convert('RGB')
im2 = image2.convert('RGB')
im3 = image3.convert('RGB')
imagelist = [im1,im2,im3]
im1.save(r'C:/Users/uidg3972/Desktop/New folder/mergedImages.pdf',save_all=True,
append_images=imagelist)