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.
pip install openpyxl
Python code
71 linesimport 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
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
- Use pandas DataFrames with the built-in Excel writer and chart support for larger datasets
- 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
More from Files & data
- Audit File Permissions Across a Project in Python easy
- Automatically Detect Corrupted Files Using SHA-256 Checksums in Python easy
- Automatically Highlight Data Validation Errors Inside Excel Files in Python easy
- Build a Command-Line To-Do List Application with Data Persistence in Python easy
- Build a Personal Work Hours Tracker in Python medium
- Build a Python Script That Detects and Deletes Empty Files Across Folders easy
Keep learning
Related tutorials and quizzes for this topic.