Python 3 Script to Convert Single or Multiple Images to PDF Documents Using img2pdf and pillow Module Full Project For Beginners

 

Welcome folks today in this blog post we will be converting single or multiple images to pdf documents using img2pdf and pillow module in python 3.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

In order to get started you need to install the below libraries using the pip command as shown below

 

 

pip install img2pdf

 

pip install pillow

 

 

After installing this libraries make an index.py file and copy paste the following code

 

 

index.py

 

 

# Python3 program to convert image to pfd
# using img2pdf library

# importing necessary libraries
import img2pdf
from PIL import Image
import os

# storing image path
img_path = "C:/Users/Admin/Desktop/GfG_images/do_nawab.png"

# storing pdf path
pdf_path = "C:/Users/Admin/Desktop/GfG_images/file.pdf"

# opening image
image = Image.open(img_path)

# converting into chunks using img2pdf
pdf_bytes = img2pdf.convert(image.filename)

# opening or creating pdf file
file = open(pdf_path, "wb")

# writing pdf files with chunks
file.write(pdf_bytes)

# closing image file
image.close()

# closing pdf file
file.close()

# output
print("Successfully made pdf file")

Leave a Reply