Managing employee leave manually is inefficient and error-prone. By automating leave requests, approvals, and tracking with Python, MongoDB, and Pandas, businesses can improve efficiency and reduce administrative workload.
Lillqvist Strat specializes in automation that streamlines HR processes, saving time and minimizing errors.
1. Leave Management Basics
An automated leave management system should:
- Store leave records in MongoDB.
- Automate leave request approvals using Python logic.
- Analyze leave trends using Pandas.
- Send notifications for leave requests and approvals.
2. MongoDB Leave Records
We use MongoDB to store leave requests, track employee leave balances, and ensure data integrity.
Setting Up the Database
from pymongo import MongoClient
client = MongoClient("mongodb://localhost:27017/")
db = client["leave_management"]
leave_collection = db["leave_requests"]
Defining the Leave Request Schema
leave_request = {
"employee_id": "EMP123",
"name": "John Doe",
"leave_type": "Annual",
"start_date": "2025-03-01",
"end_date": "2025-03-05",
"status": "Pending",
"reason": "Vacation"
}
leave_collection.insert_one(leave_request)
3. Python Approval Logic
Leave requests should be approved or rejected based on leave balance, team availability, and company policies.
Automated Leave Approval Process
def approve_leave(employee_id):
leave_request = leave_collection.find_one({"employee_id": employee_id, "status": "Pending"})
if leave_request:
leave_collection.update_one(
{"employee_id": employee_id, "status": "Pending"},
{"$set": {"status": "Approved"}}
)
print(f"✅ Leave approved for {leave_request['name']}.")
else:
print("No pending leave request found.")
approve_leave("EMP123")
Rejecting Leave Requests
def reject_leave(employee_id, reason):
leave_request = leave_collection.find_one({"employee_id": employee_id, "status": "Pending"})
if leave_request:
leave_collection.update_one(
{"employee_id": employee_id, "status": "Pending"},
{"$set": {"status": "Rejected", "rejection_reason": reason}}
)
print(f"❌ Leave request rejected for {leave_request['name']}. Reason: {reason}")
else:
print("No pending leave request found.")
reject_leave("EMP123", "High workload during requested period")
4. Pandas Leave Trends Analysis
To analyze leave patterns and optimize workforce planning, we use Pandas.
import pandas as pd
# Load leave data into Pandas
leave_data = list(leave_collection.find({}, {"_id": 0}))
df = pd.DataFrame(leave_data)
# Analyze leave types
leave_summary = df.groupby("leave_type")["employee_id"].count()
print(leave_summary)
# Export leave records to Excel
df.to_excel("leave_report.xlsx", index=False)
5. Automated Notifications
Employees and managers should receive notifications for leave approvals and rejections.
Sending Email Notifications
import smtplib
from email.mime.text import MIMEText
def send_notification(email, subject, message):
sender_email = "your_company@example.com"
password = "yourpassword"
msg = MIMEText(message)
msg["Subject"] = subject
msg["From"] = sender_email
msg["To"] = email
with smtplib.SMTP("smtp.example.com", 587) as server:
server.starttls()
server.login(sender_email, password)
server.sendmail(sender_email, email, msg.as_string())
# Example: Notify employee of approved leave
send_notification("employee@example.com", "Leave Request Approved", "Your leave request has been approved.")
Conclusion
Automating leave management with Python, MongoDB, and Pandas eliminates manual errors, streamlines approvals, and provides data-driven insights into employee leave trends.
Lillqvist Strat helps businesses implement automated HR solutions that save time and improve efficiency. Let’s automate your leave management today!

Lillqvist Strat consults on business developement, software projects, automation, SOPs, analytical tools and more.
Contact me today to get started on our journey to higher profits, more revenue and happier employees!
Go to Contact now