Chapter 8: Advanced Automation - Creating Pivot Tables

You've learned how to create beautifully formatted reports with charts, but one of Excel's most powerful features for data analysis is the Pivot Table. Pivot tables allow you to summarize large amounts of data into a compact, insightful table, making it easy to see patterns and totals.

In this chapter, we will take our automation to the next level by learning how to create a pivot table programmatically.

The Goal

Our goal is to take the sales data from the report we generated in Chapter 7 and create a pivot table that summarizes the total revenue for each product category. This will give us a high-level view of our sales performance.

A Powerful New Tool: The pandas Library

While openpyxl is excellent for reading, writing, and styling Excel files, it doesn't have a direct way to create pivot tables. For this task, we will introduce a new, incredibly powerful library called pandas.

pandas is the most popular library in the Python world for data manipulation and analysis. It's the perfect tool for this job. Before we start, you'll need to install it. Open your terminal or command prompt and run:

pip install pandas

You'll also need to install the engine that pandas uses to interact with Excel files, which is openpyxl (already installed) or another one called XlsxWriter. It's good practice to have XlsxWriter as well.

pip install XlsxWriter

The Script: create_pivot_table.py

We will use the monthly_sales_report.xlsx file as our data source. Create a new Python script named create_pivot_table.py.

The process will be:

  1. Use pandas to read the data from the "Sales Data" sheet into a special structure called a DataFrame.

  2. Use the pivot_table function in pandas to summarize the data.

  3. Write this new pivot table to a new sheet in our existing report file.

Here is the complete script:

Run the script. When it's finished, open your monthly_sales_report.xlsx file. You will find a new sheet named "Sales Summary" containing a perfectly summarized pivot table showing the total revenue for each category.

What You've Accomplished

You have now stepped into the world of advanced data analysis with Python. You've learned how to integrate the pandas library into your workflow to perform complex data aggregations like pivot tables. This combination of pandas for data crunching and openpyxl for reporting and styling is the standard for professional-level Excel automation.

Last updated