-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithubpogui.py
90 lines (71 loc) · 2.96 KB
/
githubpogui.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
from tkinter import *
import tkinter as tk
from tkinter import filedialog
window=Tk()
window.title("PO Reader Alpha")#Title
def newcompany(): #for future companies input
pass
def jojorun(): #company A when run
import re
import pdfplumber
import pandas as pd
from collections import namedtuple
file = filedialog.askopenfilename(filetypes = ((("PDF", "*.pdf"), )))
def loadtemplate():
dirname = filedialog.askdirectory(parent=window, initialdir="/", title = 'Please select a destination')
Line = namedtuple('Line', 'Company PO_Number IDx Item_No Variant_Code Description Age In_Years_Or_Months Qty Unit_Cost Disc Amount')
#Finding the line by using Item No. formate (using regex)
line_re = re.compile(r'\b[A-Z]{2}\d{3}\b')
company_re = re.compile(r'(Company A)')
purchase_re = re.compile(r'(\b[A-Z]{4}\d{8}\b)')
#address_re is not being used right now
address_re = re.compile(r'(DELIVERY ADDRESS)')
lines = []
total_check = 0
with pdfplumber.open(file) as pdf:
pages = pdf.pages
for page in pdf.pages:
text=page.extract_text()
for line in text.split('\n'):
purchase = purchase_re.search(line)
comp = company_re.search(line)
if comp:
company_name = comp.group(1)
elif purchase:
po_no = purchase.group(1)
elif line_re.search(line):
parts = line.split()
items = parts[:3] + [" ".join(parts[3:-6])] + parts[-6:]
lines.append(Line(company_name, po_no, *items))
destinationkey = dirname + '/' + po_no + '.xlsx'
df = pd.DataFrame(lines)
destinationkey = destinationkey.replace("/", "\\")
df.to_excel(destinationkey, sheet_name='PO', index = False)
success_label = tk.Label(window, text="Success!")
success_label.grid(row=2, column=0)
output_button = tk.Button(window, text="Select Destination & Convert", command = loadtemplate, width=28)
output_button.grid(row=0, column=4)
entry_company = Entry(window, text="Enter Company Name")
#user can input either "company a" or just "a"
entry_company.grid(row=0, column=0)
def quit():
window.quit() #for closing programme function
def company_input():
input_company = entry_company.get()
#makes it so that the input is not case sensitive
x = input_company.lower()
if x == "company a" or x== "a":
import_button = tk.Button(window, text="Import PDF", command=jojorun)
import_button.grid(row=0, column = 2)
newlabel = tk.Label(window, text="Company A is selected")
newlabel.grid(row=1, column=0)
enter_button = tk.Button(window, text="Enter", command=company_input)
enter_button.grid(row=0, column=1)
info = tk.Label(window, text="PO Reader v.Alpha")
info.grid(row=4, column=1)
close_button = tk.Button(window, text="Close", command=quit)
close_button.grid(row=4, column=0)
#currently servers no function but is a placeholder for a future drag and drop mechanic
list1 = Listbox(window, height=6, width=35)
list1.grid(row=3, column=0)
window.mainloop()