Search This Blog

Generating Reports and Dashboards

 📈 Generating Reports and Dashboards with Python and Google Sheets

Creating reports and dashboards manually can be a repetitive and time-consuming task. But with Python + Google Sheets, you can automate data collection, formatting, and visualization, all inside a collaborative cloud spreadsheet!

This guide walks you through how to:

✅ Automatically generate reports
✅ Update Google Sheets with dynamic data
✅ Create simple dashboards using built-in charts
✅ Schedule automated updates


🧰 Tools You’ll Need

  • Python 3.x

  • Google Sheets

  • gspread for interacting with Google Sheets

  • oauth2client for authentication

  • (Optional) matplotlib or pandas for processing/visualizing data before export

Install Dependencies

pip install gspread oauth2client pandas matplotlib

🔑 Step 1: Authenticate with Google Sheets

Set up your Google Sheets API access using a service account (see previous post on Working with Google Sheets API).

import gspread
from oauth2client.service_account import ServiceAccountCredentials

scope = ["https://spreadsheets.google.com/feeds", "https://www.googleapis.com/auth/drive"]
creds = ServiceAccountCredentials.from_json_keyfile_name("credentials.json", scope)
client = gspread.authorize(creds)
sheet = client.open("Sales Report").sheet1

📤 Step 2: Populate the Sheet with Dynamic Report Data

Imagine you want a weekly sales report. Here’s a sample:

import pandas as pd

# Example data
data = {
    "Date": ["2025-04-13", "2025-04-14", "2025-04-15"],
    "Product": ["Widget A", "Widget B", "Widget A"],
    "Quantity": [12, 8, 5],
    "Revenue": [240, 160, 100]
}

df = pd.DataFrame(data)

# Clear existing sheet
sheet.clear()

# Update headers and data
sheet.append_row(df.columns.tolist())
for row in df.values.tolist():
    sheet.append_row(row)

📊 Step 3: Create a Dashboard Inside Google Sheets

Once your data is there:

  1. Go to Insert > Chart in Google Sheets

  2. Choose chart type: Column, Line, Pie, etc.

  3. Link chart to your data range (e.g., A1:D4)

  4. Place charts in a separate Dashboard sheet

You can automate this partially with Google Apps Script if needed, or manually design the dashboard using named ranges and Google Sheets’ visual tools.


📅 Step 4: Schedule Automated Reports

Option A: Use Python Script + Cron Job (Linux/macOS)

Edit crontab with:

crontab -e

Add:

0 9 * * 1 /usr/bin/python3 /path/to/your_script.py

This runs every Monday at 9 AM.

Option B: Windows Task Scheduler

Use Task Scheduler to run your .py script weekly or daily.


🖼 Optional: Export Matplotlib Charts to Image and Upload

If you generate charts using matplotlib, you can save them locally and upload them to Google Drive (using pydrive or Google Drive API), then link to them from your sheet.

import matplotlib.pyplot as plt

df.groupby("Product")["Revenue"].sum().plot(kind='bar')
plt.title("Revenue by Product")
plt.savefig("revenue_chart.png")

🔁 Automating KPIs or Summary Rows

Add a summary section to the sheet:

summary_data = [
    ["Total Revenue", df["Revenue"].sum()],
    ["Average Sale", df["Revenue"].mean()],
    ["Top Product", df.groupby("Product")["Revenue"].sum().idxmax()]
]

for row in summary_data:
    sheet.append_row(row)

✅ Final Thoughts

You’ve just built a pipeline to:

  • Collect or generate dynamic data

  • Export it to Google Sheets

  • Build live dashboards and summaries

  • Automate the entire workflow

Feature Tool
Sheet Integration gspread
Data Processing pandas
Charting Google Sheets / matplotlib
Automation cron / Task Scheduler

Popular Posts