-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExtracting_text_from_PDF.py
More file actions
52 lines (45 loc) · 1.39 KB
/
Copy pathExtracting_text_from_PDF.py
File metadata and controls
52 lines (45 loc) · 1.39 KB
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
47
48
49
import pdf2image
import os,sys
try:
from PIL import Image
except ImportError:
import Image
import pytesseract
PATH = 'Enter your path'
i=1
def delete_ppms():
for file in os.listdir(PATH):
if '.ppm' in file or '.DS_store' in file:
try:
os.remove(PATH + file)
except FileNotFoundError:
pass
pdf_files = []
docx_files = []
for f in os.listdir(PATH):
full_name = os.path.join(PATH,f)
if os.path.isfile(full_name):
name=os.path.basename(f)
filename,ext = os.path.slitex(name)
if ext == '.pdf':
pdf_files.append(name)
elif ext == ('.docx'):
docx_files.append(name)
def pdf_extact(file,i):
print("extacting from file.",file)
delete_ppms()
images = pdf2image.convert_from_path(PATH + file,output_folder = PATH)
j=0
for file in sorted(os.listdir(PATH)):
if '.ppm' in file and 'image' not in file:
os.rename(PATH + file,PATH + 'image'+str(i)+'-'+str(j)+'.ppm')
j += 1
j=0
f = open(PATH + 'result{}.txt'.format(i),'w')
files = [f for f in os.listdir(PATH) if '.ppm' in f]
for file in sorted(files,key=lambda x: int(x[x.index('-') + 1: x.index('.')])):
temp = pytesseract.image_to_string(Image.open(PATH + file))
f.write(temp)
f.close()
for i in range(len(pdf_files)):
pdf_file = pdf_files[i]