forked from muskaancontlo/Pdf-Search-Engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpdfReader.py
46 lines (33 loc) · 1.08 KB
/
pdfReader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import PyPDF2
import json
#create file object variable
#opening method will be rb
pdffileobj=open('AU127-1.pdf','rb')
#create reader variable that will read the pdffileobj
pdfreader=PyPDF2.PdfReader(pdffileobj)
#This will store the number of pages of this pdf file
x=len(pdfreader.pages)
#create a variable that will select the selected number of pages
# pageobj=pdfreader.pages[x-1]
#(x+1) because python indentation starts with 0.
#create text variable which will store all text datafrom pdf file
# text=pageobj.extract_text()
text = ""
print(type(text))
for i in range(x):
pageobj=pdfreader.pages[i-1]
pg = pageobj.extract_text()
text += "/n"
text += pg
#save the extracted data from pdf to a txt file
#we will use file handling here
#dont forget to put r before you put the file path
#go to the file location copy the path by right clicking on the file
#click properties and copy the location path and paste it here.
#put "\\your_txtfilename"
file1=open(r"AU127-1.txt","a")
file1.writelines(text)
json = json.dump(file1)
f = open("text.json", 'w')
f.write(json)
f.close()