Pivot Tables API

pyopenxlsx can create Data Pivot Tables from source ranges and attach Slicers. Prefer the high-level PivotTableBuilder for everyday use; native XLPivotTableOptions remains available for advanced control.

Important Setup Rule: When generating a pivot table from scratch, place it on a different worksheet from the source data, and ensure target_cell does not include a worksheet name (cell only, e.g. "B3"). The source range must include the sheet name (e.g. "SalesData!A1:F4").

Native XLPivotTableOptions (advanced)

from pyopenxlsx import Workbook
from pyopenxlsx._openxlsx import XLPivotTableOptions, XLPivotSubtotal, XLSlicerOptions

with Workbook() as wb:
    ws_data = wb.active
    ws_data.name = "SalesData"
    # ... write source data ...
    ws_pivot = wb.create_sheet("PivotSheet")

    options = XLPivotTableOptions("SalesPivot", "SalesData!A1:F4", "B3")
    (
        options
        .add_filter_field("Date")
        .add_row_field("Region")
        .add_column_field("Product")
        .add_data_field("Revenue", "Total Revenue ($)", XLPivotSubtotal.Sum, 4)
        .set_pivot_table_style("PivotStyleMedium14")
    )
    ws_pivot.add_pivot_table(options)

    slicer_opts = XLSlicerOptions()
    slicer_opts.name = "RegionSlicer"
    slicer_opts.caption = "Filter by Region"
    pivot = ws_pivot._sheet.get_pivot_table("SalesPivot")
    ws_pivot._sheet.add_pivot_slicer("E3", pivot, "Region", slicer_opts)

    wb.save("pivot_native.xlsx")

Native fluent methods

  • add_row_field / add_column_field / add_filter_field / add_data_field

  • Layout: set_pivot_table_style, set_show_row_stripes, set_compact_data, grand totals, etc.

XLSlicerOptions

  • name, caption, width / height, offset_x / offset_y