Creating Subtotals in Excel using Python

This quick guide explains how to add subtotal in Excel using Python. It has details to set the IDE, a list of steps, and a sample code to for creating subtotals in Excel using Python. You will also learn code enhancements and improvisations for generating the custom subtotal output.

Steps to Create Automatic Subtotals using Python

  1. Set the IDE to use Aspose.Cells for Python via .NET to create subtotals
  2. Apply the license, create a workbook, and access the first worksheet
  3. Add sample headers and create sample grouped data into a 2D array
  4. Insert the sample data under the headers starting from row 2
  5. Define the range and the header row
  6. Apply subtotal grouped by region and subtotal on the amount column
  7. Set the subtotal rows below the details rows by setting the summary_row_below property
  8. Set the collapse state of the outline levels in the output Excel file
  9. Auto-fit the column width and save the resultant workbook as an Excel file on the disk

The above steps summarize how to make subtotal in Excel using Python. Configure the IDE, apply a license, create a workbook and access the first sheet, add appropriate headers in the sheet, set sample grouped data in a two-dimensional array, and insert it into the sheet according to the headers. Set the range and the header row, apply a subtotal to the data grouped by regions and a subtotal for the amount column, display the summary below the row, and set outline levels in the Excel file before saving it on the disk.

Code to Create Automatic Subtotals in Excel using Python

This code demonstrates how to create subtotals in Excel using Python. You may use various overloads exposing replace, page_breaks, and summary_below_data for controlling the behavior of subtotals, such as clearing the existing subtotals, setting the page breaks, and displaying the summary above or below the details. Options are also available to add multiple subtotal columns (e.g. amount, count, average) by passing multiple column numbers in the total_list.

This article has guided us to work with subtotals in an Excel file. For linking a slicer with multiple pivot tables, refer to the article Link slicer to multiple pivot tables using Python.