Python Script to Extract Text From PDF Using PyPDF2 Library in Terminal
51 views
Jun 3, 2025
Buy the full source code of application here:
View Video Transcript
0:00
uh hello guys uh welcome to this video
0:02
so in this video I will show you a
0:04
Python script which will actually
0:06
extract the text from the PDF document
0:10
uh so we have this simple PDF document
0:13
present right here in the same directory
0:15
sample PDF it contains two pages and we
0:18
will try to extract all this text and
0:21
try to print this in the
0:22
terminal so this is really easy there is
0:25
a package here that you just need to
0:26
install it's a very popular package py
0:29
pdf2 so just go to the command line just
0:32
execute this command pip install py pdf2
0:37
so I have already installed it and then
0:39
after installing it I will just show you
0:43
how to use
0:45
this so first of all just import this
0:48
package like this from py pdf2 we just
0:54
need to
0:54
import this class here which is PDF
0:59
reader after that as you can see it
1:02
contains this PDF reader class here from
1:05
py PDF2 and then we just need to
1:07
initialize this so for initializing it
1:11
we will be passing the path of the
1:14
PDF so to this class here PDF reader and
1:18
simply pass the full path of the PDF
1:20
file so it is present in the same
1:22
directory so we have passed the simple
1:24
name here
1:25
sample.pdf after that there is a
1:27
function which is there we declare a
1:30
variable text where we'll be storing it
1:33
and then we will loop through all the
1:36
pages total number of pages like
1:39
this so this object here it contains
1:43
pages right here which will contain the
1:44
total number of pages so we are running
1:47
a simple for loop it will loop through
1:49
all the pages and then we will
1:52
extract so there is a function right
1:54
here which is extract text so it will
1:58
extract all the text from the PDF
2:00
document and I will just print this text
2:03
in the next line so print text so now
2:07
what happens if I execute this script
2:09
here python app
2:12
py so for this you just need to sorry
2:16
just make sure that this P is
2:20
capital so just make sure the spelling
2:23
is correct and then now run
2:26
this you will see all the text has been
2:29
successfully extracted you can see
2:35
that so in this easy way guys you can
2:38
extract all the text from the PDF
2:40
document using this package py PDF2
2:44
inside
2:45
Python uh thank you very much for
2:48
watching this video and also check out
2:50
my website which is
2:54
freemediatools.com which contains
2:56
thousands of tools
#Programming
#Software