Turn Excel Data Into an Interactive Dashboard With Python and Pyecharts
Pyecharts renders an Excel dataset as a standalone HTML dashboard: sales and profit by month, by subcategory and by other cuts, all interactive and all in one file you can email. pandas loads the spreadsheet, Pyecharts builds and arranges the charts.
Table of contents
Load the Excel data
pd.read_excel loads the sheet into a pandas DataFrame. Tell it the file path, the sheet name, and which rows and columns hold the table, so titles above the table and notes beside it stay out. Reading .xlsx files needs openpyxl, so run pip install openpyxl once.
In the video I generate this code with my Excel add-in MyToolBelt, but it’s not a requirement: you can type it yourself. MyToolBelt is a Windows add-in, and its notebook export is part of the Pro version.
from pathlib import Path # Python Standard Library
import pandas as pd # pip install pandas openpyxl
# Read Excel file (Financial_Data.xlsx) into a pandas DataFrame.
df = pd.read_excel(
io= Path.cwd() / "Financial_Data.xlsx",
engine= "openpyxl",
sheet_name= "Orders",
skiprows= 2,
usecols= "B:T",
nrows= 3312,
)
Install Pyecharts
Pyecharts is the Python wrapper for Apache ECharts, a JavaScript chart library. Install it with pip, then import the options module and the three classes this dashboard uses: Bar, Calendar and Tab.
pip install pyecharts
from pathlib import Path # Python Standard Library
import pandas as pd # pip install pandas openpyxl
from pyecharts import options as opts # pip install pyecharts
from pyecharts.charts import Bar, Calendar, Tab
Prepare the data
df.info() shows that no column has missing values and that the order dates are already datetime values. That makes the monthly view easy: add a Month column from the order date, then group by month and sum sales and profit.
# Get a quick info about our dataframe
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3312 entries, 0 to 3311
Data columns (total 19 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 Order ID 3312 non-null object
1 Order Date 3312 non-null datetime64[ns]
2 Ship Mode 3312 non-null object
3 Customer ID 3312 non-null object
4 Customer Name 3312 non-null object
5 Segment 3312 non-null object
6 Country 3312 non-null object
7 City 3312 non-null object
8 State 3312 non-null object
9 Postal Code 3312 non-null int64
10 Region 3312 non-null object
11 Product ID 3312 non-null object
12 Category 3312 non-null object
13 Sub-Category 3312 non-null object
14 Product Name 3312 non-null object
15 Sales 3312 non-null float64
16 Quantity 3312 non-null int64
17 Discount 3312 non-null float64
18 Profit 3312 non-null float64
dtypes: datetime64[ns](1), float64(3), int64(2), object(13)
memory usage: 491.8+ KB
# Add month column to our dataframe
df['Month'] = df['Order Date'].dt.month
# group dataframe by months
grouped_by_months = df.groupby(by=['Month'])[['Sales', 'Profit']].sum()
grouped_by_months.head(3)
Sales Profit
Month
1 43971.3740 7140.4391
2 20301.1334 1613.8720
3 58872.3528 14751.8915
Sales and profit by month
A Pyecharts chart is built by chaining methods on a chart object. For the bar chart, the months go on the x-axis and sales and profit become two series. Pyecharts expects plain Python lists, so each pandas Series is converted with tolist(). render_notebook() shows the chart inside Jupyter.
# plot sales & profit by months
bar_chart_by_month = (
Bar()
.add_xaxis(grouped_by_months.index.tolist())
.add_yaxis('Sales', grouped_by_months['Sales'].round(0).tolist())
.add_yaxis('Profit', grouped_by_months['Profit'].round(0).tolist())
.set_global_opts(
title_opts=opts.TitleOpts(title='Sales & Profit by month', subtitle='in USD')
)
)
bar_chart_by_month.render_notebook()
Sales and profit by subcategory
Subcategory names are long, so this chart uses horizontal bars. The data is grouped by sub-category and sorted by sales. reversal_axis() swaps the axes, and the labels sit to the right of each bar.
# group data by sub-category
grouped_by_sub_category = df.groupby(by='Sub-Category', as_index=False)[['Sales', 'Profit']].sum().sort_values(by=['Sales'])
# plot sales & profit by subcategroy
bar_chart_by_subcategory = (
Bar()
.add_xaxis(grouped_by_sub_category['Sub-Category'].tolist())
.add_yaxis('Sales', grouped_by_sub_category['Sales'].round(0).tolist())
.add_yaxis('Profit', grouped_by_sub_category['Profit'].round(0).tolist())
.reversal_axis()
.set_series_opts(label_opts=opts.LabelOpts(position='right'))
.set_global_opts(
title_opts=opts.TitleOpts(title='Sales & Profit by subcategory', subtitle='in USD')
)
)
bar_chart_by_subcategory.render_notebook()

Sales calendar heatmap
A calendar heatmap shows the sales of every single day of the year. Group the sales by order date, round them, and turn the result into a list of date and value pairs, which is the format the Calendar chart expects. The range sets the year, and the visual map sets the color scale.
The code takes the color scale from the smallest and largest single order. Days with several orders can exceed that maximum and all get the darkest color. Use sales_by_date['Sales'].max() and .min() instead if you want the scale to match the daily totals. If you prefer Plotly, the plotly-calplot package draws the same kind of calendar heatmap in one call.
# Plot sales calendar
sales_by_date = df.groupby(by='Order Date')[['Sales']].sum().round(0)
sales_by_date = sales_by_date.reset_index()
data = sales_by_date[['Order Date', 'Sales']].values.tolist()
# calculate min / max sales
max_sales = df['Sales'].max()
min_sales = df['Sales'].min()
sales_calendar = (
Calendar()
.add('', data, calendar_opts=opts.CalendarOpts(range_='2021'))
.set_global_opts(
title_opts=opts.TitleOpts(title='Sales Calendar', subtitle='in USD'),
legend_opts=opts.LegendOpts(is_show=False),
visualmap_opts=opts.VisualMapOpts(
max_=max_sales,
min_=min_sales,
orient='horizontal',
is_piecewise=False,
pos_top='230px',
pos_left='100px',
)
)
)
sales_calendar.render_notebook()
Export one HTML file
A Tab object puts all charts into one HTML file, with one tab per chart. Add each chart with a tab label and render the Tab to a file path. The result is a single file you can email, and it opens in any browser.
# Create HTML file to combine all charts
tab = Tab(page_title='Sales & Profit Overview')
tab.add(bar_chart_by_month, 'Sales & Profit by months')
tab.add(bar_chart_by_subcategory, 'Sales & Profit by subcategory')
tab.add(sales_calendar, 'Sales Calendar')
tab.render(Path.cwd() / 'Sales & Profit Overview.html')

The file loads the ECharts library from the pyecharts asset server, so the charts need an internet connection when the file is opened. For KPI tiles on top, the Pyecharts liquid chart fits into the same Tab or Grid layout.
Not every dashboard needs code. The free dashboard maker builds a shareable dashboard from pasted Excel cells in the browser, and I compare no-code options inside Excel in turning Excel data into interactive dashboards without coding. If you want to stay inside Excel, the free xlwings Lite add-in runs pyecharts too: I use it for interactive Excel charts with Python, and its AI assistant can write a whole dashboard for you.
