Mathew K Analytics

Lesson 20 · Data analytics zero to hero

Excel Pivot Tables & Charts for Reporting | Data Analytics #20

Video twenty of the 30-part series, wrapping up the Excel block: multi-sheet workbooks, pivot-style summaries, and genuine native Excel charts, still…

What you'll learn

Datasets used in this lesson

Save these next to the notebook. In Google Colab, upload them with the 📁 icon on the left first.

📓 Full notebook

Download .ipynb

Data Analytics Zero to Hero, Video 20: Excel Pivot-Style Reports and Charts#

  • Video twenty of the 30-part series, wrapping up the Excel block: multi-sheet workbooks, pivot-style summaries, and genuine native Excel charts, still entirely in Python.
  • Continuing with the real Sample Superstore dataset.
  • Let's jump straight in.

Before You Start#

  • Open a new Jupyter Notebook in VS Code and select your Python interpreter as the kernel.
  • Place superstore_sales.csv in the same folder as this notebook.

Part 1: A Multi-Sheet Workbook with pandas#

import pandas as pd

df = pd.read_csv('superstore_sales.csv')
df['Order Date'] = pd.to_datetime(df['Order Date'])

category_pivot = df.pivot_table(values='Sales', index='Region', columns='Category', aggfunc='sum').round(2)
monthly_trend = df.set_index('Order Date')['Sales'].resample('ME').sum().round(2).reset_index()
print(category_pivot)
Category  Furniture  Office Supplies  Technology
Region                                          
Central   163797.16        167026.42   170416.31
East      208291.20        205516.06   264973.98
South     117298.68        125651.31   148771.91
West      252612.74        220853.25   251991.83
with pd.ExcelWriter('superstore_report.xlsx', engine='openpyxl') as writer:
    df.head(200).to_excel(writer, sheet_name='Raw Data', index=False)
    category_pivot.to_excel(writer, sheet_name='By Region and Category')
    monthly_trend.to_excel(writer, sheet_name='Monthly Trend', index=False)
print('Real multi-sheet workbook written')
Real multi-sheet workbook written

Part 2: Reopening the Workbook to Add Charts#

import openpyxl
wb = openpyxl.load_workbook('superstore_report.xlsx')
print(wb.sheetnames)
['Raw Data', 'By Region and Category', 'Monthly Trend']

Part 3: A Native Bar Chart#

from openpyxl.chart import BarChart, Reference

pivot_ws = wb['By Region and Category']
print(pivot_ws.dimensions)
chart = BarChart()
chart.title = 'Real Sales by Region and Category'
chart.y_axis.title = 'Sales'
chart.x_axis.title = 'Region'
A1:D5
data = Reference(pivot_ws, min_col=2, max_col=pivot_ws.max_column, min_row=1, max_row=pivot_ws.max_row)
categories = Reference(pivot_ws, min_col=1, min_row=2, max_row=pivot_ws.max_row)
chart.add_data(data, titles_from_data=True)
chart.set_categories(categories)
pivot_ws.add_chart(chart, 'H2')
print('Real bar chart added')
Real bar chart added

Part 4: A Native Line Chart#

from openpyxl.chart import LineChart

trend_ws = wb['Monthly Trend']
line_chart = LineChart()
line_chart.title = 'Real Monthly Sales Trend'
line_chart.y_axis.title = 'Sales'
line_data = Reference(trend_ws, min_col=2, min_row=1, max_row=trend_ws.max_row)
line_chart.add_data(line_data, titles_from_data=True)
trend_ws.add_chart(line_chart, 'D2')
print('Real line chart added')
Real line chart added

Part 5: A Native Pie Chart#

from openpyxl.chart import PieChart

category_totals = df.groupby('Category')['Sales'].sum().round(2).reset_index()
pie_ws = wb.create_sheet('Category Share')
pie_ws.append(['Category', 'Sales'])
for row in category_totals.itertuples(index=False):
    pie_ws.append(list(row))
print(category_totals)
          Category      Sales
0        Furniture  741999.80
1  Office Supplies  719047.03
2       Technology  836154.03
pie_chart = PieChart()
pie_chart.title = 'Real Sales Share by Category'
pie_data = Reference(pie_ws, min_col=2, min_row=1, max_row=pie_ws.max_row)
pie_labels = Reference(pie_ws, min_col=1, min_row=2, max_row=pie_ws.max_row)
pie_chart.add_data(pie_data, titles_from_data=True)
pie_chart.set_categories(pie_labels)
pie_ws.add_chart(pie_chart, 'D2')
print('Real pie chart added')
Real pie chart added

Part 6: Saving and Verifying#

wb.save('superstore_report.xlsx')
print('Real, finished report saved')
Real, finished report saved
check_wb = openpyxl.load_workbook('superstore_report.xlsx')
print(check_wb.sheetnames)
print(f"Charts on By Region and Category: {len(check_wb['By Region and Category']._charts)}")
print(f"Charts on Monthly Trend: {len(check_wb['Monthly Trend']._charts)}")
print(f"Charts on Category Share: {len(check_wb['Category Share']._charts)}")
['Raw Data', 'By Region and Category', 'Monthly Trend', 'Category Share']
Charts on By Region and Category: 1
Charts on Monthly Trend: 1
Charts on Category Share: 1

Wrap-Up: What You Learned#

  • Writing multiple real DataFrames to separate sheets in one workbook with pandas' ExcelWriter.
  • Reopening a pandas-written file with openpyxl to add features pandas alone can't: native charts.
  • BarChart, LineChart, and PieChart, all built the same way: define the chart, Reference the real data and categories, add_data, and add_chart.
  • Creating a brand new sheet with create_sheet.
  • Saving and reopening to verify every real chart genuinely persisted.
  • This wraps up the Excel block. Video twenty-one starts a new block on working with real APIs and live web data in Python. Subscribe so it lands automatically see you there.

Found this useful?

All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.