-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemail_extractor.py
More file actions
65 lines (52 loc) · 2.14 KB
/
email_extractor.py
File metadata and controls
65 lines (52 loc) · 2.14 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
from tkinter import *
from tkinter import messagebox
import re
#main window paramters
root = Tk()
root.title("Email Address Extractor")
root.geometry('1500x900+200+200')
# function to search input string for regex matches, store in list and then
# create unique set of matches in new list. It then inputs these to a textbox on the gui form.
def get_emails(s):
''' gets matches and inputs to textbox'''
Emails = [re.findall("([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\\.[a-zA-Z0-9-.]+)", s)]
for Email in Emails:
email_set = set(Email)
Txtbox_2.insert(1.0, email_set)
# event handler for the process button. clears output textbox on each run
# and takes input from input texbox and passes it to the get_emails function.
def button_click():
'''button event handler for process button'''
Txtbox_2.delete(1.0, END)
line = Txtbox_1.get("1.0", END)
get_emails(line)
# function to allow exporting to txt file with try/except checks. it will append or
# create file foo.txt in the current working directory and write each value in the output
# textbox to the file, opening a message box on completion or generating exception on fail.
def export_txt():
'''export to txt file'''
try:
output_data = [Txtbox_2.get("1.0", 'end -1c')]
with open("output.txt", "w+") as output_file:
for val in output_data:
output_file.write(val)
messagebox.showinfo("Alert", "Export Complete")
except:
print('An error occurred.')
# basic form paramters
app = Frame(root)
app.grid(padx=40, pady=40)
Lbl_1 = Label(app, text="Enter text here: ")
Lbl_1.grid(row=7, column=2)
Lbl_2 = Label(app, text="Addresses Detected: ")
Lbl_2.grid(row=7, column=5)
Bttn_1 = Button(app, command=lambda: button_click(), text="Process Text")
Bttn_1.grid(row=9, column=2)
Bttn_2 = Button(app, command=lambda: export_txt(), text="Export")
Bttn_2.grid(row=9, column=5)
Txtbox_1 = Text(app, width=50, height=25, relief=RIDGE, borderwidth=2, bg='white')
Txtbox_1.grid(row=6, column=2)
Txtbox_2 = Text(app, width=50, height=25, relief=RIDGE, borderwidth=2, bg='white')
Txtbox_2.grid(row=6, column=5)
# initiate main loop
mainloop()