Python 3 PyPDF2 Script to Count the Pages inside PDF Document & Display it in Terminal
Jun 3, 2025
Get the full source code of application here:
https://codingshiksha.com/python/python-3-pypdf2-script-to-count-the-pages-inside-pdf-document-display-it-in-terminal/
Show More Show Less 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
count the total number of pages inside a
0:06
PDF document inside this Python script
0:10
for this we are using this library py
0:12
PDF2 which is a PDF processing library
0:14
inside Python open source so here you
0:17
simply provide the file name we are
0:20
passing this file name as you can see
0:22
merged PDF it has got a total of 28
0:26
pages so if I try to execute this Python
0:29
script it will basically count the total
0:32
number of pages and it will give the
0:34
total number of pages is
0:36
28 so in this easy way you can do this
0:40
so first of all you just need to install
0:42
this package so py pdf2 so simply search
0:47
for this
0:50
package so the command is simple you
0:53
simply install this package after
0:56
installing it
1:00
uh just create a simple app py file and
1:03
first of all you just need to import
1:05
this
1:07
module like this py pdf2 after that you
1:12
simply open the input file where you
1:15
working with so merged
1:17
PDF we will read this file in read
1:20
binary mode
1:22
flag as file and then we will read
1:27
this and it contains a method right here
1:31
which is called as
1:34
PDF reader this is the method and here
1:39
we simply pass the PDF file and once it
1:43
contains a property here which is called
1:47
as length and right here you will pass
1:51
this PDF reader and it contains a
1:54
property called as pages so then it will
1:56
return the total number of pages which
1:58
are present inside this uh so here we
2:01
can simply print that total pages
2:05
so it will print out this value that's
2:07
all that we need to do very simple we
2:10
simply import the module then we pass it
2:13
open this file then we read this and
2:16
then it returns this pages property so
2:19
if you run this again you will see total
2:21
number of pages is 28 you can change
2:24
this let's suppose I provide a different
2:26
file this one contains two pages
2:34
so you can see that so this is the easy
2:36
way by which inside Python you can count
2:38
the total number of pages inside pi uh
2:41
the PDF file uh also check out my
2:45
website
2:47
freemediattools.com uh which contains
2:49
thousands of tools
