-
Notifications
You must be signed in to change notification settings - Fork 0
/
task2.py
74 lines (64 loc) · 2.3 KB
/
task2.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
import openpyxl
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication
# Global variables
excel_file_path = "attendance.xlsx"
email_sender = "[email protected]"
email_password = "your_password"
email_subject = "Attendance Report"
threshold = 80 # Adjust this threshold as needed
absentees = []
# Initialize the Excel sheet and load data
def initialize_excel():
global wb, sheet
try:
wb = openpyxl.load_workbook(excel_file_path)
sheet = wb.active
except Exception as e:
print(f"Error initializing Excel: {str(e)}")
# Save the Excel sheet after updating
def save_excel():
try:
wb.save(excel_file_path)
wb.close()
except Exception as e:
print(f"Error saving Excel: {str(e)}")
# Function to track attendance and update the absentees list
def track_attendance(student_name, attendance_percentage):
global absentees
if attendance_percentage < threshold:
absentees.append(student_name)
print(f"{student_name} is marked absent. Absentees: {absentees}")
# Function to send an email with attendance report
def send_email():
global absentees
try:
message = MIMEMultipart()
message["From"] = email_sender
message["Subject"] = email_subject
body = f"Attendance report for today:\n\n"
if not absentees:
body += "All students are present."
else:
body += "Absent students:\n"
for student in absentees:
body += f"- {student}\n"
message.attach(MIMEText(body, "plain"))
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login(email_sender, email_password)
server.sendmail(email_sender, email_sender, message.as_string())
server.quit()
print("Email sent successfully.")
except Exception as e:
print(f"Error sending email: {str(e)}")
if __name__ == "__main__":
initialize_excel()
# Simulate attendance updates - you can replace this with your data
track_attendance("Student1", 75)
track_attendance("Student2", 90)
track_attendance("Student3", 70)
save_excel()
send_email()