Python 3 img2pdf Library to Export All Images in Directory to PDF Document in Terminal
18 views
Jun 3, 2025
Get the full source code of application here: https://codingshiksha.com/python/python-3-img2pdf-library-to-export-all-images-in-directory-to-pdf-document-in-terminal/
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you how to
0:04
convert a bunch of images which is
0:07
present inside a directory into PDF
0:10
document inside Python so we will be
0:12
using this module third party module
0:14
which uh image to PDF uh so this module
0:18
allows you to convert a bunch of images
0:20
into PDF so we have these images present
0:24
right here so these JPG and PNG images
0:27
so we have a folder here where these
0:31
images are present so now I will just
0:33
run this Python script here so as I run
0:36
this you will see on the left hand side
0:37
it will create this output PDF file here
0:41
so all these images are being converted
0:45
to PDF here so all the nine images you
0:48
will see each image is taking the full
0:50
space of a page
0:53
here so I will show you how to write the
0:56
script here all the source code is given
0:58
in the description of the video so first
1:00
of all you just need to install this
1:03
module which I'm this image to PDF so
1:07
img2 PDF this is actually the module
1:14
here so the command is simple uh simply
1:17
install this i have already installed it
1:20
so now just import this module here
1:22
import image to PDF then you need to
1:25
also import the operating system module
1:28
and here you just need to specify the
1:30
directory where the images are present
1:32
so I will just provide the directory
1:34
name and then the PDF path so output PDF
1:39
path here so output PDF file will get
1:41
created so here we'll be specifying the
1:45
extensions
1:46
all the extensions will
1:48
be so here we are specifying here either
1:52
it can be jpg jpg orpng all these image
1:57
files will be converted so now we will
2:01
be running a simple for loop so write
2:05
here operating system module dotpath dot
2:09
join and here we'll be getting all the
2:13
files which are present inside this
2:15
directory which is image
2:22
directory so here we'll be running a
2:25
simple for loop for f in
2:30
sorted operating system dot list
2:33
directory and uh providing the image
2:36
directory that's
2:38
all after that we simply need
2:44
to write this we'll sort
2:48
this so you'll be checking this uh
2:52
extensions only we'll be looping through
2:55
all the files which are present inside
2:56
this directory so only these extensions
2:59
JPG JPG or PNG files will be there after
3:04
getting access to the images now we can
3:06
simply convert them into PDF so
3:14
uh in the else scenario we'll be simply
3:16
be converting this so first of all we
3:18
will
3:19
be opening the output PDF path in the
3:24
right binary
3:27
mode and then we'll be using the right
3:30
function right here so you'll be using
3:32
this image to PDF library it contains
3:35
this function here convert and here
3:37
we'll be passing this image files array
3:40
here
3:43
and after that you can simply write a
3:45
print statement that
3:48
uh successfully created the PDF file so
3:52
now if I delete this file here and rerun
3:56
the application you will see on the left
3:59
hand side it will create this output
4:02
PDF so all the images that was there in
4:05
the directory has been successfully
4:06
converted to a PDF file so in this easy
4:09
way you can install this module and use
4:11
it inside your Python application
4:13
directly in the terminal to actually
4:15
convert multiple images into PDF
4:19
so thank you very much for watching this
4:21
video and please hit that like button
4:23
subscribe the channel and also check out
4:25
my website free media tools.com uh which
4:29
contains thousands of tools