How To Refresh Pivot Tables Using OpenPyXL?

Method

pivot=ws._pivots[0]    #Reference the 1st Pivot Table

dt[‘F2′]=’China’    #Modify Data Source

#Refresh Pivot Table

pivot.cache.refreshOnLoad=True

Sample Code

#Refresh Pivot Table

from openpyxl import load_workbook

#Load Workbook
wb=load_workbook('Pivot.xlsx')
dt=wb['Data Source']
ws=wb['Pivot Table']

pivot=ws._pivots[0]    #Reference the 1st Pivot Table
dt['F2']='China'    #Modify Data Source
#Refresh Pivot Table
pivot.cache.refreshOnLoad=True

wb.save('test.xlsx')
Refresh Pivot Tables Using OpenPyXL

How To Reference Pivot Tables Using OpenPyXL?

Method

pivot=ws._pivots[0]    #Reference the 1st Pivot Table

print(pivot.name)    #Name of the Pivot Table

Sample Code

#Reference Pivot Table

from openpyxl import load_workbook

#Load Workbook
wb=load_workbook('Pivot.xlsx')
#Get Worksheet
ws=wb['Pivot Table']

pivot=ws._pivots[0]    #Reference the 1st Pivot Table
print(pivot.name)    #Name of the Pivot Table

#Pivot Table Overview
print(pivot.summary)

wb.save('test.xlsx')
Reference Pivot Tables Using OpenPyXL