简体   繁体   中英

Sorting a pivot table with win32com using Python

I would like to sort an Excel pivot table created by using the win32com module with Python.

As I understood, I should use the function AutoSort() and according to the official documentation , there are four fields, two of which are optional. Therefore, I am specifying only the "Order" and "Field" fields.

AutoSort(Order=1, Field="Tot Converted Amount Due")

However, when I run the below line of code, I get an error.

    wb.Sheets("pivot_table").PivotTables("pivot_table").PivotFields("Tot Converted Amount Due").AutoSort(Order=1, Field="Tot Converted Amount Due")

Error:

def AutoSort(self, Order=defaultNamedNotOptArg, Field=defaultNamedNotOptArg, PivotLine=defaultNamedOptArg, CustomSubtotal=defaultNamedOptArg):
---> 44         return self._oleobj_.InvokeTypes(1514, LCID, 1, (24, 0), ((3, 1), (8, 1), (12, 17), (12, 17)),Order
     45             , Field, PivotLine, CustomSubtotal)
     46 

com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)

What am I doing wrong?

I would really appreciate any inputs or help. Thanks!

I don't know if this is your case, but you need to pass a row field in the .PivotFields("""Row field""") .

Have you tried sorting with ascending=true instead? As in https://stackoverflow.com/a/60778942/18247317

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM