-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoiceExtract.py
More file actions
589 lines (485 loc) · 22.9 KB
/
Copy pathInvoiceExtract.py
File metadata and controls
589 lines (485 loc) · 22.9 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
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
import pdfplumber
import os
import re
import json
import csv
import openpyxl as xl
from openpyxl.styles import PatternFill, Alignment, Border, Side
import pandas as pd
from collections import OrderedDict
import utils
from google.cloud import vision
import tempfile
def create_temp_key(api_key: dict):
"Create temp key file"
f = tempfile.NamedTemporaryFile(mode="w+", delete=False)
json.dump(api_key, f)
f.flush()
os.system("attrib +h " + f.name)
return f
def authorize_google_vision(api_key_file: tempfile._TemporaryFileWrapper):
"Authorize Google Vision API"
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = api_key_file.name
def delete_temp_key(api_key_file):
"Delete temp key file"
try:
api_key_file.close()
os.unlink(api_key_file.name)
except FileNotFoundError:
print(f"File {api_key_file.name} not found")
API_KEY = # YOUR API KEY HERE
f = create_temp_key(API_KEY)
authorize_google_vision(f)
client = vision.ImageAnnotatorClient()
BULLETIN_KEYWORDS = ("Bulletin Co.,Ltd", " ")
METRO_KEYWORDS = ("เมโทร ยูนิฟอร์ม", " ")
LEEKA_KEYWORDS = ("ลีก้า", "LEEKA", " ")
BBL_KEYWORDS = ("Currency THB Date", "By the instruction of", "Beneficiary name :", "Beneficiary Account :",
"Invoice details as follows (if any)", "Payment Net")
KBANK_KEYWORDS = ("KASIKORNBANK PCL", "On behalf of", "Payment details are as follows")
MAPPING = json.load(open("mapping.json", "r", encoding='utf-8'))
class PDFInvoice:
# Invoice metadata
pdf = None
invoice_types = ['Credit Advice Report',
'Pre-Advice Report', 'Payment Advice']
invoice_extension = ''
invoice_type = ''
text = ''
# Invoice data
receiver = None
bank = None
receiver_account = None
payment_date = None
total_after_tax = None
cheque_id = None
sender = None
bank_charge = None
def close(self) -> None:
self.pdf.close()
def get_invoice_info(self) -> None:
pass
def extract(self, file: str, mode: str = "records") -> None:
pass
def get_entries(self, mode: str = "records") -> dict:
data = {
"บริษัท (ผู้รับเงิน)": self.receiver,
"ธนาคาร": self.bank,
"เลขที่บัญชีธนาคารบริษัทที่โอนเข้า": self.receiver_account,
"วันที่ชำระ": self.payment_date,
"จำนวนเงินที่ชำระ": self.total_after_tax,
"เลขที่เช็ค": self.cheque_id,
"ชื่อลูกค้า": self.sender,
"items": self.extract(self.file_path, mode),
"ค่าธรรมเนียมธนาคาร (ถ้ามี)": self.bank_charge
}
return data
def __init__(self, file_path: str) -> None:
self.info = None
self.file_path = file_path
if file_path.endswith('.pdf'):
self.invoice_extension = 'pdf'
self.pdf = pdfplumber.open(file_path)
self.get_invoice_info()
else:
raise Exception('File type not supported')
def to_txt(self) -> None:
with open(f'output/{os.path.basename(self.file_path)}.txt', 'w', encoding='utf-8') as f:
for page in self.info:
f.write(f'[PAGE {page.page_number}]\n') if page.page_number == 1 else f.write(
f'\n[PAGE {page.page_number}]\n')
f.write(page.extract_text())
print(f'"{os.path.basename(self.file_path)}.txt" written to output folder')
def to_json(self) -> None:
data = self.get_entries()
utils.pretty_save_json(
f'output/{os.path.basename(self.file_path)}.json', data)
print(f'"{os.path.basename(self.file_path)}.json" written to output folder\n')
def to_excel(self) -> None:
if not os.path.exists(r'output/temp'):
os.mkdir(r'output/temp')
file = self.get_entries(mode="list")
metadata = {k: v for k, v in file.items() if k != "items" and k !=
"ค่าธรรมเนียมธนาคาร (ถ้ามี)"}
items = file["items"]
fee = file["ค่าธรรมเนียมธนาคาร (ถ้ามี)"]
header = [i for i in metadata.keys()] + [i for i in items.keys()] + \
["ค่าธรรมเนียมธนาคาร (ถ้ามี)"]
# to csv
with open(f"output/temp/{os.path.basename(self.file_path)}.csv", "w", encoding="utf-8", newline="") as f:
writer = csv.writer(f)
writer.writerow(header)
if len(items["เลขที่ Invoice"]) > 0:
for i in range(len(items["เลขที่ Invoice"])):
if i == 0:
writer.writerow([metadata[k] for k in metadata.keys()] +
[items[k][i] for k in items.keys()] + [fee])
else:
writer.writerow([None for _ in metadata.keys()] +
[items[k][i] for k in items.keys()])
else:
writer.writerow([metadata[k] for k in metadata.keys()] +
[None for _ in items.keys()] + [fee])
df = pd.read_csv(f"output/temp/{os.path.basename(self.file_path)}.csv", encoding="utf-8")
df.to_excel(f"output/{os.path.basename(self.file_path)}.xlsx", index=False)
os.remove(f"output/temp/{os.path.basename(self.file_path)}.csv")
# Excel operation
cols = ["A", "B", "C", "D", "E", "F", "G", "N"]
null_fill = PatternFill(patternType="solid", fgColor="D9D9D9")
normal_align = Alignment(horizontal="left", vertical="top")
border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
header_fill = PatternFill(patternType="solid", fgColor="FFFF99")
wb = xl.load_workbook(f"output/{os.path.basename(self.file_path)}.xlsx")
ws = wb["Sheet1"]
c = ws["C2"]
ws.freeze_panes = c
# merge cells
for col in cols:
try:
ws.merge_cells(f"{col}2:{col}{len(items['เลขที่ Invoice']) + 1}")
except ValueError:
pass
# set border
for column_cells in ws.columns:
length = max(len(str(cell.value)) for cell in column_cells)
ws.column_dimensions[column_cells[0].column_letter].width = length
# styling
for row in ws.iter_rows():
for cell in row:
if cell.row == 1:
cell.fill = header_fill
if cell.value is None:
cell.fill = null_fill
cell.alignment = normal_align
cell.border = border
wb.save(f"output/{os.path.basename(self.file_path)}.xlsx")
print(f'"{os.path.basename(self.file_path)}.xlsx" written to output folder\n')
def correct_words(text: str, mapping: dict) -> str:
for word in mapping:
text = text.replace(word, mapping[word])
return text
class KBANKInvoice(PDFInvoice):
def parse_row(self, row):
return OrderedDict([
("INV.NUMBER", row[0:18].strip()),
("INV.DATE", row[18:32].strip()),
("INV.AMOUNT", row[32:45].strip()),
("VAT AMT", row[45:55].strip()),
("Amt. (รวม Vat)", ""),
("WHT AMT", row[55:65].strip()),
("NET AMOUNT", row[65:].strip())
])
def extract(self, path: str, mode="records") -> dict:
pdf = pdfplumber.open(path)
p0 = pdf.pages[1]
text = p0.extract_text()
core_pat = re.compile(r"NET AMOUNT\n=+\n(.*)\n=+\nTOTAL", re.DOTALL)
core = re.search(core_pat, text).group(1)
core = core.split("\n")
parsed = [self.parse_row(x) for x in core]
cols = list(parsed[0].keys())
data = pd.DataFrame(parsed, columns=cols)
data = data.drop(columns="INV.DATE")
data = data.rename(columns={"INV.NUMBER": "เลขที่ Invoice", "INV.AMOUNT": "Amt. (ก่อน Vat)",
"VAT AMT": "Vat. Amt", "WHT AMT": "WHT Amt. (แต่ละ Inv)",
"NET AMOUNT": "จำนวนเงินสุทธิ (แต่ละ Inv)"})
data = data.replace([''], [None])
data_dict = data.to_dict(orient=mode)
return data_dict
def get_invoice_info(self) -> None:
self.info = self.pdf.pages
for page in self.info:
self.text += page.extract_text()
self.text = correct_words(self.text, MAPPING)
type_match = re.search(r"(Subject : )(\w)+", self.text)
if type_match:
self.invoice_type = type_match.group(2)
date_match = re.search(
r"(Cheque Date : )(\d{2}/\d{2}/\d{4})", self.text)
if date_match:
self.payment_date = date_match.group(2)
else:
print(
f'Payment date not found in {os.path.basename(self.file_path)}')
sender_match = re.search(
r"(Payer Name\s+: )([\w ()ก-๛.,]+)", self.text)
if sender_match:
self.sender = sender_match.group(2)
else:
print(
f'Sender name not found in {os.path.basename(self.file_path)}')
receiver_match = re.search(r"(To : )([\w ()ก-๛.,]+)", self.text)
if receiver_match:
self.receiver = receiver_match.group(2)
else:
print(
f'Receiver name not found in {os.path.basename(self.file_path)}')
total_match = re.search(
r"(Total Invoice after VAT : \*+)([\d,.]+)", self.text)
if total_match:
self.total_after_tax = total_match.group(2)
else:
print(
f'Total after tax not found in {os.path.basename(self.file_path)}')
bank_charge_match = re.search(
r"(Benef Charges : \*+)([\d,.]+)", self.text)
if bank_charge_match:
self.bank_charge = bank_charge_match.group(2)
self.bank_charge = self.bank_charge.replace(".00", "0")
def __init__(self, file_path: str) -> None:
super().__init__(file_path)
self.bank = 'กสิกรไทย (KBANK)'
class BBLInvoice(PDFInvoice):
credit_advice_cols = ["Item No", "Invoice No.", "Date",
"Gross Amount", "WHT Amount", "VAT Amount", "Income Type"]
pre_advice_cols = ["Item No", "Invoice No.",
"Date", "Gross Amount", "WHT Amount"]
def get_invoice_info(self) -> None:
self.info = self.pdf.pages
for page in self.info:
self.text += page.extract_text()
if all(x in self.text for x in self.credit_advice_cols):
self.invoice_type = self.invoice_types[0]
elif all(x in self.text for x in self.pre_advice_cols):
self.invoice_type = self.invoice_types[1]
if self.invoice_type not in self.invoice_types:
raise ValueError(
f'Invoice type not found in {os.path.basename(self.file_path)}')
date_match = re.search(
r'(Payment Date : )(\d{2}-\w{3}-\d{2})', self.text)
if date_match:
self.payment_date = date_match.group(2)
else:
print(
f'Payment Date not found in {os.path.basename(self.file_path)}')
sender_match = re.search(
r'(By the instruction of : )([\w ()ก-๛.,]+)', self.text)
if sender_match:
self.sender = sender_match.group(2)
else:
print(f'Sender not found in {os.path.basename(self.file_path)}')
receiver_match = re.search(
r'(Beneficiary name : )([\w ()ก-๛.,]+)', self.text)
if receiver_match:
self.receiver = receiver_match.group(2)
else:
print(f'Receiver not found in {os.path.basename(self.file_path)}')
receiver_account_match = re.search(
r'(Beneficiary [Aa]ccount : )(\d+)', self.text)
if receiver_account_match:
self.receiver_account = receiver_account_match.group(2)
total_match = re.search(r'(Payment Net : )([\d,.]+)', self.text)
if total_match:
self.total_after_tax = total_match.group(2)
cheque_id_match = re.search(r'(Cheque No. : )(\d+)', self.text)
if cheque_id_match:
self.cheque_id = cheque_id_match.group(2)
def __init__(self, file_path: str) -> None:
super().__init__(file_path)
self.bank = 'กรุงเทพ (BBL)'
def extract(self, file: str, mode: str = "records") -> dict:
pdf = pdfplumber.open(file)
cols = self.credit_advice_cols if self.invoice_type == 'Credit Advice Report' else self.pre_advice_cols
data = pd.DataFrame(columns=cols)
for page in pdf.pages:
table = page.extract_table()
if type(table) != list:
return {
"เลขที่ Invoice": [],
"Amt. (ก่อน Vat)": [],
"Vat. Amt": [],
"Amt. (รวม Vat)": [],
"WHT Amt. (แต่ละ Inv)": [],
"จำนวนเงินสุทธิ (แต่ละ Inv)": []
}
# Filter out empty rows
table = list(filter(lambda a: a != ['', '', '', '', '', '', ''] and a != [
'', '', '', '', ''], table))
# replace \n with space in the table
for i in range(len(table)):
for j in range(len(table[i])):
table[i][j] = table[i][j].replace('\n', ' ')
if page.page_number == 1 and self.invoice_type == 'Credit Advice Report':
df = pd.DataFrame(table[0:-1], columns=cols)
else:
df = pd.DataFrame(
table[1:-1], columns=cols) if self.invoice_type == 'Credit Advice Report' else pd.DataFrame(table,
columns=cols)
data = pd.concat([data, df], ignore_index=True)
data = data.drop(0)
data = data[["Invoice No.", "Gross Amount", "WHT Amount"]]
placeholder = [None for _ in range(len(data))]
data.insert(1, "Amt. (ก่อน Vat)", placeholder)
data.insert(2, "Vat. Amt", placeholder)
data["จำนวนเงินสุทธิ (แต่ละ Inv)"] = placeholder
data = data.rename(columns={"Invoice No.": "เลขที่ Invoice",
"Gross Amount": "Amt. (รวม Vat)", "WHT Amount": "WHT Amt. (แต่ละ Inv)"})
data = data.replace([''], [None])
# if all the invoice data are None, return empty dict
if all(data.isna().all()):
return {
"เลขที่ Invoice": [],
"Amt. (ก่อน Vat)": [],
"Vat. Amt": [],
"Amt. (รวม Vat)": [],
"WHT Amt. (แต่ละ Inv)": [],
"จำนวนเงินสุทธิ (แต่ละ Inv)": []
}
data_dict = data.to_dict(orient=mode)
return data_dict
class ImgInvoice:
supplier_name = None
date = None
invoice_no = None
sub_total = None
grand_total = None
vat_total = None
text = ""
def __init__(self, image_path):
# Image Preprocessing
self.image_to_show = image_path
image_to_scan = utils.preprocess(image_path)
content = utils.get_content(image_path, image_to_scan)
# Text Extraction
image = vision.Image(content=content)
self.response = client.text_detection(image=image)
self.annotations = self.response.text_annotations
self.full_text_annotation = self.response.full_text_annotation
self.text = str(self.annotations[0].description)
if not os.path.exists(r'output'):
os.mkdir(r'output')
def get_entries(self):
entries = {
"ชื่อ Supplier": self.supplier_name,
"วันที่ในเอกสาร": self.date,
"เลขที่เอกสาร": self.invoice_no,
"Amt. (ก่อน Vat)": self.sub_total,
"Vat. Amt": self.vat_total,
"Amt. (รวม Vat)": self.grand_total,
}
return entries
def to_json(self):
entries = self.get_entries()
utils.pretty_save_json(f"{os.path.basename(self.image_to_show)}.json", entries)
def to_excel(self):
entries = self.get_entries()
df = pd.DataFrame(entries, index=[0])
df.to_excel(f"output/{os.path.basename(self.image_to_show)}.xlsx", index=False)
null_fill = PatternFill(patternType="solid", fgColor="D9D9D9")
normal_align = Alignment(horizontal="left", vertical="top")
border = Border(left=Side(style='thin'),
right=Side(style='thin'),
top=Side(style='thin'),
bottom=Side(style='thin'))
header_fill = PatternFill(patternType="solid", fgColor="FFFF99")
wb = xl.load_workbook(f"output/{os.path.basename(self.image_to_show)}.xlsx")
ws = wb["Sheet1"]
ws.freeze_panes = ws['A2']
# set border
for column_cells in ws.columns:
length = max(len(str(cell.value)) for cell in column_cells)
ws.column_dimensions[column_cells[0].column_letter].width = length
# styling
for row in ws.iter_rows():
for cell in row:
if cell.row == 1:
cell.fill = header_fill
if cell.value is None:
cell.fill = null_fill
cell.alignment = normal_align
cell.border = border
wb.save(f"output/{os.path.basename(self.image_to_show)}.xlsx")
print(f'"{os.path.basename(self.image_to_show)}.xlsx" written to output folder\n')
class BulletinInvoice(ImgInvoice):
def __init__(self, image_path):
super().__init__(image_path)
self.supplier_name = "Bulletin Co.,Ltd"
def get_invoice_info(self):
text = self.text
float_sub_total = None
float_grand_total = None
# Find payment date
date_match = re.search(r"Date :[\n ]*(\w+ \d+, \d+)", text)
if date_match:
self.date = date_match.group(1)
# Find invoice number
inv_no_match = re.search(r"Inv. No :[\n ]*([\w\d -]+)", text)
if inv_no_match:
self.invoice_no = inv_no_match.group(1)
# Find sub total
sub_total_match = re.search(r"Sub-Total[\n ]*([\d,.]+)", text)
if sub_total_match:
self.sub_total = sub_total_match.group(1)
float_sub_total = utils.to_float(self.sub_total)
# Find grand total
grand_total_match = re.search(r"Grand Total[\n ]*([\d,.]+)", text)
if grand_total_match:
self.grand_total = grand_total_match.group(1)
float_grand_total = utils.to_float(self.grand_total)
# Calculate VAT total
if float_sub_total is not None and float_grand_total is not None:
float_vat_total = float_grand_total - float_sub_total
self.vat_total = utils.to_string(float_vat_total)
class MetroUniformInvoice(ImgInvoice):
def __init__(self, image_path):
super().__init__(image_path)
self.supplier_name = "Metro Uniform Co.,Ltd"
def get_invoice_info(self):
text = self.text
float_sub_total = None
float_grand_total = None
# Find payment date
date_match = re.search(r"วันที่[\n ]*(\d{2}/\d{2}/\d{4})", text)
if date_match:
self.date = date_match.group(1)
# Find invoice number
inv_no_match = re.search(r"เลขที่[\n ]*(IV\d+-\d+)", text)
if inv_no_match:
self.invoice_no = inv_no_match.group(1)
# Find sub total
sub_total_match = re.search(r"รวมเงิน[\n ]*([\d,.]+)", text)
if sub_total_match:
self.sub_total = sub_total_match.group(1)
float_sub_total = utils.to_float(self.sub_total)
# Find grand total
grand_total_match = re.search(r"จำนวนเงินทั้งสิ้น[\n ]*[ก-๛ ]*[\n ]*([\d,.]+)", text)
if grand_total_match:
self.grand_total = grand_total_match.group(1)
float_grand_total = utils.to_float(self.grand_total)
# Calculate VAT total
if float_sub_total is not None and float_grand_total is not None:
float_vat_total = float_grand_total - float_sub_total
self.vat_total = utils.to_string(float_vat_total)
class LeekaInvoice(ImgInvoice):
def __init__(self, image_path):
super().__init__(image_path)
self.supplier_name = "Leeka Business Co.,Ltd"
def get_invoice_info(self):
text = self.text
float_sub_total = None
float_grand_total = None
# Find payment date
date_match = re.search(r"(วันที่|Date)[\n ]*(\d{2}/\d{2}/\d{4}|\d{2}/\d{2}/\d{2})", text)
if date_match:
self.date = date_match.group(2)
# Find invoice number
inv_no_match = re.search(r"(เลขที่)*[ก-๛]*[\n ]*(IV\d+|SR\d+)", text)
if inv_no_match:
self.invoice_no = inv_no_match.group(2)
# Find sub total
sub_total_match = re.search(r"(รวม|ยอดรวม[\n ]*TOTAL)[\n ]*([\d,.]+)", text)
if sub_total_match:
self.sub_total = sub_total_match.group(2)
float_sub_total = utils.to_float(self.sub_total)
# Find grand total
grand_total_match = re.search(r"(รวมทั้งสิ้น|NET TOTAL)[\n ]*([\d,.]+)", text)
if grand_total_match:
self.grand_total = grand_total_match.group(2)
float_grand_total = utils.to_float(self.grand_total)
# Calculate VAT total
if float_sub_total is not None and float_grand_total is not None:
float_vat_total = float_grand_total - float_sub_total
self.vat_total = utils.to_string(float_vat_total)