Python 6 min read

Automating Daily Reports with Pandas

Mike Ross avatar

Contributotor

Automating Daily Reports with Pandas
Featured image for Automating Daily Reports with Pandas

Stop wasting time on Excel. Learn how to write a simple Python script to fetch, clean, and email your daily business metrics.

Stop the Manual Grind

If you’re still copying and pasting data into Excel every morning to create reports, you’re wasting valuable time. Let’s automate this process with Python and Pandas.

The Setup

First, install the required packages:

Code
pip install pandas openpyxl smtplib

Fetching Your Data

Whether it’s from a database, API, or CSV file, Pandas makes it easy:

Code
import pandas as pd
import requests

# From CSV
df = pd.read_csv('sales_data.csv')

# From API
response = requests.get('https://api.example.com/metrics')
df = pd.DataFrame(response.json())

# From database
import sqlalchemy
engine = sqlalchemy.create_engine('postgresql://user:pass@localhost/db')
df = pd.read_sql('SELECT * FROM sales WHERE date = CURRENT_DATE', engine)

Data Cleaning

Clean and transform your data with Pandas:

Code
# Remove duplicates
df = df.drop_duplicates()

# Handle missing values
df['revenue'] = df['revenue'].fillna(0)

# Convert types
df['date'] = pd.to_datetime(df['date'])

# Calculate metrics
df['profit_margin'] = (df['revenue'] - df['cost']) / df['revenue'] * 100

Creating the Report

Generate a formatted Excel report:

Code
from openpyxl.styles import Font, PatternFill

# Create summary statistics
summary = df.groupby('category').agg({
    'revenue': 'sum',
    'units_sold': 'sum',
    'profit_margin': 'mean'
}).round(2)

# Write to Excel with formatting
with pd.ExcelWriter('daily_report.xlsx', engine='openpyxl') as writer:
    summary.to_excel(writer, sheet_name='Summary')
    df.to_excel(writer, sheet_name='Raw Data', index=False)

    # Format the summary sheet
    workbook = writer.book
    worksheet = workbook['Summary']

    # Style headers
    for cell in worksheet[1]:
        cell.font = Font(bold=True)
        cell.fill = PatternFill(start_color="366092", fill_type="solid")

Automating Email Delivery

Send the report via email automatically:

Code
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders

def send_report(to_email, filename):
    msg = MIMEMultipart()
    msg['From'] = 'reports@company.com'
    msg['To'] = to_email
    msg['Subject'] = f'Daily Sales Report - {pd.Timestamp.now().date()}'

    # Attach the file
    with open(filename, 'rb') as f:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(f.read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', f'attachment; filename={filename}')
    msg.attach(part)

    # Send email
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login('your@email.com', 'your_password')
    server.send_message(msg)
    server.quit()

send_report('manager@company.com', 'daily_report.xlsx')

Schedule It

Use cron (Linux/Mac) or Task Scheduler (Windows) to run this script daily:

Code
# Cron example - runs at 9 AM every day
0 9 * * * /usr/bin/python3 /path/to/report_script.py

Conclusion

With this automated system, you’ll save hours each week and ensure consistent, accurate reporting. Your morning coffee just got a lot more enjoyable!

Related Articles

More articles coming soon...

Discussion (14)

Sarah J Sarah Jenkins

Great article! The explanation of the attention mechanism was particularly clear. Could you elaborate more on how sparse attention differs in implementation?

Mike Ross Mike Ross Author

Thanks Sarah! Sparse attention essentially limits the number of tokens each token attends to, often using a sliding window or fixed patterns. I'll be covering this in Part 2 next week.

Dev Guru Dev Guru

The code snippet for the attention mechanism is super helpful. It really demystifies the math behind it.