📄 Payslip Generation & Email Automation
This Python project automates the payroll process by:
Reading employee data from an Excel file (employees.xlsx)
Generating PDF payslips for each employee
Sending the payslips to employees via email
All sensitive credentials are securely stored in a .env file.
🧰 Technologies Used
pandas — For reading Excel data
reportlab — For generating PDF documents
smtplib & email.mime — For composing and sending emails
python-dotenv — For loading environment variables from .env
📦 Required Files
employees.xlsx — Contains employee data (ID, Name, Email, Salary, Allowances, Deductions)
.env — Stores sensitive email credentials
generate_payslip.py — Logic to generate payslips
send_email.py — Main script to process employees and send payslips
📄 How the Scripts Work
🧾 generate_payslip.py
This file defines the function create_payslip() which:
Accepts employee_Id, name, salary, allowances, and deductions
Calculates Net Pay: Net Pay = Salary + Allowances - Deductions
Uses reportlab to create a clean and professional payslip
Saves it as payslip_<Employee_Name>.pdf
📬 send_email.py
This is the main script that ties everything together.
- Load Environment Variables
from dotenv import load_dotenv load_dotenv()
Retrieves EMAIL and EMAIL_PASSWORD from .env
- Read Excel File
df = pd.read_excel("employees.xlsx")
Reads employee details into a DataFrame
- Generate Payslips
for index, row in df.iterrows(): create_payslip(...)
- Iterates through each row to generate a PDF payslip
Send Emails
def send_email(to_email, subject, body, attachment_path):
Composes an email with the payslip attached
Sends it using Gmail SMTP with TLS encryption
EMAIL=[email protected] EMAIL_PASSWORD=your_app_specific_password
- Set Up Project
git clone cd payslip python -m venv my_venv source my_venv/bin/activate # or my_venv\Scripts\activate on Windows pip install -r requirements.txt
- Create .env file
EMAIL=[email protected] EMAIL_PASSWORD=your_app_specific_password
- Prepare employees.xlsx Ensure this file has columns:
Employee ID | Name | Email | Basic Salary | Allowances | Deductions
- Run the Script
python send_email.py
✅ Sample Output
✅ Saved: payslip_Tinotenda_Hove.pdf ✅ Email sent successfully to [email protected]
⚠️ Error Handling
If Excel file is missing, a clear error is shown and script exits
If payslip cannot be generated, that employee is skipped
If file attachment fails, email is not sent
If credentials are wrong or missing, script fails securely
Automate payroll efficiently, securely, and with style! 💼📧
Nyanzvi_zw