Maintenance

Site is under maintenance — quizzes are still available.

Go to quizzes
Sponsored Reserved space — layout preview until AdSense is connected

Create a Python Tool That Generates Professional Excel Dashboards

Generate a professional sales dashboard in an Excel workbook with styled headers, a bar chart, and formatted number cells using the openpyxl library.

Medium Python 3.9+ Jun 28, 2026 Files & data 2 views 0 copies

Requires third-party packages — install first
pip install openpyxl

Python code

71 lines
Python 3.9+
import openpyxl
from openpyxl.chart import BarChart, Reference
from openpyxl.styles import Font, PatternFill, Alignment, Border, Side
from openpyxl.utils import get_column_letter

def create_sales_dashboard(workbook_path: str) -> None:
    """Generate a professional sales dashboard in an Excel workbook."""
    wb = openpyxl.Workbook()
    ws = wb.active
    ws.title = "Dashboard"

    # Sample data
    headers = ["Month", "Sales", "Expenses", "Profit"]
    data = [
        ["Jan", 12000, 8000, 4000],
        ["Feb", 15000, 9000, 6000],
        ["Mar", 18000, 9500, 8500],
        ["Apr", 22000, 10000, 12000],
        ["May", 25000, 11000, 14000],
        ["Jun", 30000, 12000, 18000],
    ]

    # Write headers with style
    header_font = Font(bold=True, color="FFFFFF", size=12)
    header_fill = PatternFill(start_color="2F5496", end_color="2F5496", fill_type="solid")
    thin_border = Border(
        left=Side(style='thin'),
        right=Side(style='thin'),
        top=Side(style='thin'),
        bottom=Side(style='thin')
    )
    for col_idx, header in enumerate(headers, 1):
        cell = ws.cell(row=1, column=col_idx, value=header)
        cell.font = header_font
        cell.fill = header_fill
        cell.alignment = Alignment(horizontal='center')
        cell.border = thin_border

    # Write data
    for row_idx, row_data in enumerate(data, 2):
        for col_idx, value in enumerate(row_data, 1):
            cell = ws.cell(row=row_idx, column=col_idx, value=value)
            cell.border = thin_border
            if col_idx > 1:
                cell.number_format = '#,##0'

    # Adjust column widths
    for col in range(1, len(headers) + 1):
        ws.column_dimensions[get_column_letter(col)].width = 15

    # Create bar chart
    chart = BarChart()
    chart.type = "col"
    chart.title = "Monthly Performance Summary"
    chart.y_axis.title = "Amount ($)"
    chart.x_axis.title = "Month"
    chart.style = 10

    data_ref = Reference(ws, min_col=2, min_row=1, max_col=4, max_row=len(data) + 1)
    cats_ref = Reference(ws, min_col=1, min_row=2, max_row=len(data) + 1)
    chart.add_data(data_ref, titles_from_data=True)
    chart.set_categories(cats_ref)
    chart.shape = 4

    ws.add_chart(chart, "F1")

    wb.save(workbook_path)

if __name__ == "__main__":
    create_sales_dashboard("sales_dashboard.xlsx")
    print("Dashboard created: sales_dashboard.xlsx")

Output

stdout
Dashboard created: sales_dashboard.xlsx

How it works

This script uses openpyxl to create an Excel workbook, write styled headers and sample sales data, then add a bar chart displaying monthly performance. The header styling uses a dark blue fill with white bold text and centered alignment. Number columns are formatted with comma separators for readability. The bar chart is added to a separate location (cell F1) so it doesn't overlap the data table. Finally, the workbook is saved to the specified path.

The chart shows Sales, Expenses, and Profit per month, with the month names as categories. The bar chart style is set to a predefined openpyxl style (10), and the chart has labeled axes for clarity. This approach creates a polished, presentation-ready dashboard with minimal code.

Common mistakes

  • Forgetting to install openpyxl with `pip install openpyxl` before running the script
  • Overlapping the chart with data by placing it in a cell range that's not empty (the script deliberately uses F1 to avoid overlap)
  • Not including the header row in the data reference for chart titles (`titles_from_data=True` requires it)

Variations

  1. Use pandas DataFrames with the built-in Excel writer and chart support for larger datasets
  2. Create a multi-sheet dashboard with separate summary, data, and chart sheets

Real-world use cases

  • Automating monthly financial reports for a small business or startup.
  • Generating on-demand sales dashboard exports for client presentations.
  • Integrating into a CI/CD pipeline to produce weekly KPI reports from API data.

Sponsored

Sponsored Reserved space — layout preview until AdSense is connected

Run locally

This sample needs third-party packages, so it cannot run in the browser IDE. Copy the code above, install the packages shown at the top, then run it in your own Python environment.

More from Files & data

Related tutorials and quizzes for this topic.