-
Notifications
You must be signed in to change notification settings - Fork 2
/
process_pdf.py
56 lines (50 loc) · 1.81 KB
/
process_pdf.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
47
48
49
50
51
52
53
54
55
56
import io
import PyPDF2 as ppdf
import tabula
import person_details
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage
def to_text(filename):
infoDict = {}
info = info_pdf(filename)
resource_manager = PDFResourceManager()
fake_file_handle = io.StringIO()
converter = TextConverter(resource_manager, fake_file_handle)
page_interpreter = PDFPageInterpreter(resource_manager, converter)
print(filename+" in process text")
with open("./static/"+filename, 'rb') as fh:
for page in PDFPage.get_pages(fh, caching=True, check_extractable=True):
page_interpreter.process_page(page)
text = fake_file_handle.getvalue()
converter.close()
fake_file_handle.close()
infoDict['person'] = person_details.extract(text)
infoDict['pdf_details'] = info
return infoDict
#for infpo_pdf
#flist = font list
#inum = number of images
#ntable = number of tables
def info_pdf(filename):
file = open("./static/"+filename,"rb")
pdfReader = ppdf.PdfFileReader(file)
flist=[]
inum=0
for i in range(pdfReader.numPages):
pg = pdfReader.getPage(i)
if ('/XObject' in list(pg['/Resources'].keys())):
inum = inum+len(pg['/Resources']['/XObject'].keys())
for j in range(1,len(pg['/Resources']['/Font'].keys())):
z = pg['/Resources']['/Font'][list(pg['/Resources']['/Font'].keys())[j]]['/BaseFont'][1:]
if z not in flist:
flist.append(z)
file = "./static/"+filename
tables = tabula.read_pdf(file, pages = "all", multiple_tables=True)
ntable=len(tables)
info = {}
info['table_count'] = ntable
info['fonts'] = flist
info['image_count'] = inum
return info