-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmain.py
176 lines (153 loc) · 4.74 KB
/
main.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import os
from pathlib import Path
import converter, constants,run_ocr, preproces, extraction , tables,converter
from tkinter import Tk, Entry, Frame, LabelFrame, Label, Button, END
import numpy as np
import xlsxwriter
#Making directories for new file
filename=constants.filename
cur_path= Path.cwd()
p= Path("example")
read_path= Path.joinpath(p,filename)
joined_path= Path.joinpath(cur_path,"Details")
joined_path= Path.joinpath(joined_path,filename)
try:
Path.mkdir(joined_path)
print("------Directory "+filename+ " Created----------")
except OSError as er:
print("-----Directory for "+filename+" detected----------")
try:
Path.mkdir(Path.joinpath(joined_path,'Pages'))
Path.mkdir(Path.joinpath(joined_path,'Intermediates'))
Path.mkdir(Path.joinpath(joined_path,'Rows'))
except OSError as er:
print("-----Directory for "+filename+" detected----------")
file_input= Path.joinpath(cur_path,read_path)
file_output= Path.joinpath(joined_path,"Pages")
#If the format is in pdf then convert it to jpegs
if filename.split(".")[-1]=="pdf":
converter.convert_to_jpeg(file_input,file_output)
else:
converter.in_jpeg(file_input,file_output)
page= Path("Pages")
#Process page 1
one = Path.joinpath(page,"page1.jpg")
#Generating binary images and doing morphological operations
preproces.process(Path.joinpath(joined_path,one))
#Run dry tesseract on page to get output.txt
run_ocr.run_tesseract()
#Once output.txt generated extract nominal details from it
buyer=np.array([])
seller=np.array([])
invoice=np.array([])
if constants.manual_field_enable:
buyer= extraction.get_details()
else:
buyer, seller, invoice= extraction.get_details()
#Getting the tabular data of the invoice
array= tables.get_data()
#Displaying the extracted data in the GUI with tkinter
root = Tk()
# keep track of widgets
seller_tk = np.empty(seller.shape, dtype=object)
buyer_tk = np.empty(buyer.shape, dtype=object)
invoice_tk = np.empty(invoice.shape, dtype=object)
array_tk = np.empty(array.shape, dtype=object)
# top frame ----------
topframe = Frame(root)
topframe.pack()
# seller details
frame0 = LabelFrame(topframe, text="seller details")
frame0.grid(row=0, column=0)
rows = seller.shape[0]
for i in range(rows):
l = Label(frame0, text=seller[i][0])
l.grid(row=i, column=0)
seller_tk[i][0] = l
e = Entry(frame0)
e.grid(row=i, column=1)
e.insert(END, seller[i][1])
seller_tk[i][1] = e
# buyer details
frame1 = LabelFrame(topframe, text="buyer details")
frame1.grid(row=0, column=1)
rows = buyer.shape[0]
for i in range(rows):
l = Label(frame1, text=buyer[i][0])
l.grid(row=i, column=0)
buyer_tk[i][0] = l
e = Entry(frame1)
e.grid(row=i, column=1,ipadx=10)
e.insert(END, buyer[i][1])
buyer_tk[i][1] = e
# invoice details
frame2 = LabelFrame(topframe, text="invoice details")
frame2.grid(row=0, column=2)
rows = invoice.shape[0]
for i in range(rows):
l = Label(frame2, text=invoice[i][0])
l.grid(row=i, column=0)
invoice_tk[i][0] = l
e = Entry(frame2)
e.grid(row=i, column=1)
e.insert(END, invoice[i][1])
invoice_tk[i][1] = e
# bottom frame ----------
# insert table into the grid
bottomframe = LabelFrame(root, text="transactions")
bottomframe.pack()
rows = array.shape[0]
cols = array.shape[1]
for i in range(rows):
for j in range(cols):
e = Entry(bottomframe)
e.grid(row=i, column=j)
e.insert(END, array[i][j])
array_tk[i][j] = e
# functions
# generate xls file from the data in present in tk entries
path_to_write= joined_path
def genxlsx():
workbook = xlsxwriter.Workbook(Path.joinpath(path_to_write,"Invoice.xlsx"))
worksheet = workbook.add_worksheet()
row = 0
col = 0
max_row = 0
for i in seller_tk:
worksheet.write(row, col, i[0].cget('text'))
worksheet.write(row, col+1, i[1].get())
row += 1
max_row = max(max_row, row)
row = 0
col += 6
for i in buyer_tk:
worksheet.write(row, col, i[0].cget('text'))
worksheet.write(row, col+1, i[1].get())
row += 1
max_row = max(max_row, row)
row = 0
col += 6
for i in invoice_tk:
worksheet.write(row, col, i[0].cget('text'))
worksheet.write(row, col+1, i[1].get())
row += 1
max_row = max(max_row, row)
row = max_row + 2
col = 0
for i in array_tk:
for j in i:
worksheet.write(row, col, j.get())
col += 1
col = 0
row += 1
workbook.close()
# buttons
buttons = Frame(root)
buttons.pack()
gen = Button(buttons, text="GENERATE", fg="green", command=genxlsx)
gen.grid(row=0, column=0)
quit = Button(buttons, text="QUIT", fg="red", command=root.destroy)
quit.grid(row=0, column=1)
print("==========EXTRACTION FINISHED============")
# mainloop
root.mainloop()