Formatting Pivot Table

Pivot Table Appearance

How to Create a Pivot Table explains how to create a simple pivot table. This article describes how to customize a pivot table’s appearance by setting various properties:

  • Pivot table format options
  • Pivot fields format options
  • Data field format options

How to Set Pivot Table Format Options

The PivotTable class controls the overall pivot table and can be formatted in a number of ways.

How to Set the AutoFormat Type

Microsoft Excel offers a number of different pre-set report formats. Aspose.Cells for Python via .NET support these formatting options too. To access them:

  1. Set PivotTable.is_auto_format to true.
  2. Assign a formatting option from the PivotTableAutoFormatType enumeration.
from aspose.cells import Workbook
from aspose.cells.pivot import PivotTableAutoFormatType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
pivotindex = 0
# Get the first worksheet
worksheet = workbook.worksheets[0]
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report is automatically formatted
pivotTable.is_auto_format = True
# Setting the PivotTable atuoformat type.
pivotTable.auto_format_type = PivotTableAutoFormatType.REPORT5
# Saving the Excel file
workbook.save(dataDir + "output.xls")

How to Set Format Options

The code sample below shows how to format the pivot table to show grand totals for rows and columns, and how to set the report’s field order. It also shows how to set a customer string for null values.

from aspose.cells import PrintOrderType, Workbook
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report shows grand totals for rows.
pivotTable.row_grand = True
# Setting the PivotTable report shows grand totals for columns.
pivotTable.column_grand = True
# Setting the PivotTable report displays a custom string in cells that contain null values.
pivotTable.display_null_string = True
pivotTable.null_string = "null"
# Setting the PivotTable report's layout
pivotTable.page_field_order = PrintOrderType.DOWN_THEN_OVER
# Saving the Excel file
workbook.save(dataDir + "output.xls")

Formatting Look and Feel Manually

To formatting how the pivot table report looks manually, instead of using pre-set report formats, use the PivotTable.format_all(style) and PivotTable.format(row, column, style) methods. Create a style object for your desired formatting, for example:

from aspose.cells import BackgroundType, Workbook
from aspose.cells.pivot import PivotTableStyleType
from aspose.pydrawing import Color
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivot = workbook.worksheets[0].pivot_tables[0]
pivot.pivot_table_style_type = PivotTableStyleType.PIVOT_TABLE_STYLE_DARK1
style = workbook.create_style()
style.font.name = "Arial Black"
style.foreground_color = Color.yellow
style.pattern = BackgroundType.SOLID
pivot.format_all(style)
# Saving the Excel file
workbook.save(dataDir + "output.xls")

How to Set Pivot Field Format Options

The PivotField class represents a field in a pivot table and can be formatted in a number of ways. The code sample below shows how to:

  • Access row fields.
  • Setting subtotals.
  • Setting autosort.
  • Setting autoshow.

How to Set Row/Column/Page Fields Format

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldSubtotalType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Setting the PivotTable report shows grand totals for rows.
pivotTable.row_grand = True
# Accessing the row fields.
pivotFields = pivotTable.row_fields
# Accessing the first row field in the row fields.
pivotField = pivotFields[0]
# Setting Subtotals.
pivotField.set_subtotals(PivotFieldSubtotalType.SUM, True)
pivotField.set_subtotals(PivotFieldSubtotalType.COUNT, True)
# Setting autosort options.
# Setting the field auto sort.
pivotField.is_auto_sort = True
# Setting the field auto sort ascend.
pivotField.is_ascend_sort = True
# Setting the field auto sort using the field itself.
pivotField.auto_sort_field = -5
# Setting autoShow options.
# Setting the field auto show.
pivotField.is_auto_show = True
# Setting the field auto show ascend.
pivotField.is_ascend_show = False
# Setting the auto show using field(data field).
pivotField.auto_show_field = 0
# Saving the Excel file
workbook.save(dataDir + "output.xls")

How to Set Data fields format

The code sample below shows how to set display formats and number format for data fields.

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldDataDisplayFormat, PivotItemPosition
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
worksheet = workbook.worksheets[0]
pivotindex = 0
# Accessing the PivotTable
pivotTable = worksheet.pivot_tables[pivotindex]
# Accessing the data fields.
pivotFields = pivotTable.data_fields
# Accessing the first data field in the data fields.
pivotField = pivotFields[0]
# Setting data display format
pivotField.data_display_format = PivotFieldDataDisplayFormat.PERCENTAGE_OF
# Setting the base field.
pivotField.base_field_index = 1
# Setting the base item.
pivotField.base_item_position = PivotItemPosition.NEXT
# Setting number format
pivotField.number = 10
# Saving the Excel file
workbook.save(dataDir + "output.xls")

How to Clear Pivot Fields

The PivotFieldCollection has a method named clear() that allows you to clear pivot fields. Use it when you want to clear all the pivot fields in the areas, for example, page, column, row or data. The code sample below shows how to clear all the pivot fields in a data area.

from aspose.cells import Workbook
from aspose.cells.pivot import PivotFieldType
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# The path to the documents directory.
dataDir = RunExamples.GetDataDir(".")
# Load a template file
workbook = Workbook(dataDir + "Book1.xls")
# Get the first worksheet
sheet = workbook.worksheets[0]
# Get the pivot tables in the sheet
pivotTables = sheet.pivot_tables
# Get the first PivotTable
pivotTable = pivotTables[0]
# Clear all the data fields
pivotTable.data_fields.clear()
# Add new data field
pivotTable.add_field_to_area(PivotFieldType.DATA, "Betrag Netto FW")
# Set the refresh data flag on
pivotTable.refresh_data_flag = False
# Refresh and calculate the pivot table data
pivotTable.refresh_data()
pivotTable.calculate_data()
# Saving the Excel file
workbook.save(dataDir + "output.xls")