我试图用python创建一个pivot表,但无法让它在我自己的数据上工作。我已经确定了一个问题单元是示例文件中的最后一个单元。在excel中直接创建透视表时,同样的数据也可以正常工作。在
代码如下:import win32com.client as win32
win32c = win32.constants
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open("test_pivot_cache.xlsx")
excel.Visible = True
data_sheet = wb.Sheets(1)
data_sheet.Activate()
r = data_sheet.UsedRange
n_rows = r.Rows.Count
#first column only
PivotSourceRange1 = data_sheet.Range("A1:A{}".format(n_rows))
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange1, Version=win32c.xlPivotTableVersion14)
#all but last row
PivotSourceRange2 = data_sheet.Range("A1:B{}".format(n_rows - 1))
PivotCache = wb.PivotCaches().Create(So