Here is the piece of code where i am trying to change the data source in existing pivot table,I have two pivot tables in both on i want to update the source data
excel_app = win32com.client.dynamic.Dispatch("Excel.Application")
excel_app.Interactive = False
excel_app.Visible = False
excel_app.DisplayAlerts = False
xlBook = excel_app.Workbooks.Open(r C:Users70.PDeE_Python_ScriptSc_ext.xlsx )
ws = xlBook.Worksheets[ Sheet2 ]
ws.Unprotect() # IF protected
pivotCount = ws.PivotTables().Count
print("pivot count is",pivotCount)
for j in range(1, pivotCount + 1):
ws.PivotTables(j).PivotCaches.Create(SourceType=xlDatabase, SourceData="C:Users70.PDesktopE_Python_Script[Sc_ext.xlsx]Sheet1!$A$1:$AC$4601", Version=xlPivotTableVersion14)
# Put protection back on
#ws.Protect(DrawingObjects=True, Contents=True, Scenarios=True, AllowUsingPivotTables=True)
xlBook.Close(SaveChanges=1)
del xlBook
excel_app.Quit()
del excel_app