-
Notifications
You must be signed in to change notification settings - Fork 0
/
automating_task_with_python.py
154 lines (78 loc) · 3.8 KB
/
automating_task_with_python.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
from openpyxl import load_workbook
import csv
def main():
try:
#Update the email addresses in excel format
employees = Employees_Excel("employeedata.xlsx")
print("Updating emails in .xlsx...")
employees.update_all_email_domaine("@handsinhands.org")
print("Updated emails successfully \nSaving in excel file...")
employees.save_data()
print("Successfully saved\n\n")
#Update the email addresses in csv format
employees = Employees_csv("employeedata.csv")
print("Updating emails in .csv...")
employees.update_all_email_domaine("@handsinhands.org")
print("Updated emails successfully \nSaving in csv file...")
employees.save_data()
print("Successfully saved")
except FileNotFoundError as e:
print(e)
class Employees_Excel:
def __init__(self, file_path:str, name_column:int=1, email_column:int=2,
phone_column:int=3):
self.file_path = file_path
#load data from excel file
self.workbook = load_workbook(file_path)
self.worksheet = self.workbook.active
#get the number of rows or entries in the excel file
upper_bound = self.worksheet.max_row + 1
#arrange the data in a suitable phython data structure
self.data = []
for i in range(2, upper_bound):
name = self.worksheet.cell(row=i, column=name_column).value
email = self.worksheet.cell(row=i, column=email_column).value
phone = self.worksheet.cell(row=i, column=phone_column).value
#ignore empty rows
if not name and not email and not phone:
continue
self.data.append({"name":name, "email":email, "phone":phone})
def update_all_email_domaine(self, new_domaine:str):
"""Updates the domaine name for all employees in the object's data"""
for employee in self.data:
current_email = employee["email"]
name = current_email.split("@")[0]
employee["email"] = name + new_domaine
def save_data(self):
"""saves back the object's data in the original excel file format"""
for i, employee in enumerate(self.data, 2):
for c, value in enumerate(employee.values(), 1):
self.worksheet.cell(row=i, column=c).value = value
self.workbook.save(self.file_path)
class Employees_csv:
def __init__(self, file_path:str, name_index:int=0, email_index:int=1,
phone_index:int=2):
self.file_path = file_path
self.name_index = name_index
self.email_index = email_index
self.phone_index = phone_index
#load data from csv file and
#arrange it in a suitable python data structure
with open(file_path, newline="") as file:
reader = csv.reader(file)
self.header = next(reader)
self.data = [row for row in reader]
def update_all_email_domaine(self, new_domaine:str):
"""Updates the domaine name for all employees in the object's data"""
for employee in self.data:
name = employee[self.email_index].split("@")[0]
employee[self.email_index] = name + new_domaine
def save_data(self):
"""saves back the object's data in the original excel file format"""
with open(self.file_path, "w") as file:
writer = csv.writer(file, lineterminator="\n")
writer.writerow(self.header)
for row in self.data:
writer.writerow(row)
if __name__ == "__main__":
main()