from aspose.cells import SaveFormat, Workbook

#  For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
#  Load sample Excel file containing pivot table.
wb = Workbook("sampleCreateSlicerToPivotTable.xlsx")
#  Access first worksheet.
ws = wb.worksheets[0]
#  Access first pivot table inside the worksheet.
pt = ws.pivot_tables[0]
#  Add slicer relating to pivot table with first base field at cell B22.
idx = ws.slicers.add(pt, "B22", pt.base_fields[0])
#  Access the newly added slicer from slicer collection.
slicer = ws.slicers[idx]
#  Save the workbook in output XLSX format.
wb.save("outputCreateSlicerToPivotTable.xlsx", SaveFormat.XLSX)
#  Save the workbook in output XLSB format.
wb.save("outputCreateSlicerToPivotTable.xlsb", SaveFormat.XLSB)