How to solve comerror: PivotFields method of PivotTable class failed?

1.6k Views Asked by At

I'm trying to refresh a pivot table in excel and update existing filters using python's win32com. However, I'm hitting the error of:

com_error: (-214735267, 'Exception occured.', (0, 'Microsoft Excel', 'PivotFields method of PivotTable class failed', 'xlmain11.chm', 0, -2146827284), None)

Here's my code:

import win32com.client
filepath = r'test.xlsx'

office = win32com.client.Dispatch('Excel.Application')
office.DisplayAlerts = False
wb = office.Workbooks.Open(filepath, False, False, None)

ws = wb.Worksheets[0]

for i in range(1,3):
    ws.PivotTables(i).PivotCache().Refresh()

ws.PivotTables(1).PivotFields('Position_date').PivotFilters.Add2(34, None, '31/05/2021')
# 34 = xlAfterorEqualTo

wb.SaveAs(filepath, None, '', '')
office.Quit()

Appreciate any help I can get on this. Thanks.

1

There are 1 best solutions below

1
On

I had a problem with this. In my case I was getting the error because the pivot table field name did not match the source table field name.