'How to get existing pivot table names and modify them using xlwings?

I am using xlwings(python3) to automate an excel report, need to find a way to get the names of all the existing pivot tables on a given sheet and then change the source data for it, but can't find anything about it

Do you know if this is possible?

Thanks



Solution 1:[1]

It can be done using xlwings api, Pivot tables are accessed with

.api.PivotTables()

An example to display name and rename table is below.

import xlwings as xw

filename = 'pivottables.xlsx'

wb = xw.Book(filename)
ws = wb.sheets('Sheet1')

for idx, pt in enumerate(ws.api.PivotTables()):
    print(pt.Name)
    pt.Name = "PTable" + str(idx)

wb.save("modded_" + filename)
wb.close()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 moken