I try to make excel file with Python. For that I use win32com because that seem the best way to make pivot table (pandas is not a good solution), but I have some troubles.
First when I try to make a pivot table on Sheet where there is already a pivot table i have that error message:
Traceback (most recent call last):
File "<ipython-input-55-62ca0c0e21ec>", line 1, in <module>
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
File "C:\Users\LPLOUV~1\AppData\Local\Temp\gen_py\3.7\00020813-0000-0000-C000-000000000046x0x1x9\PivotCache.py", line 45, in CreatePivotTable
, TableName, ReadData, DefaultVersion)
com_error: (-2147352567, 'Une exception s’est produite.', (0, None, None, None, 0, -2146827284), None)
Second I need to remake some pivot table that exist already, so I need to delete some pivote table from sheet but I don't know how I can make that.
Until there I use this code:
import os
import win32com.client
import time
time_first = time.time()
os.chdir(r'C:\Users\msmith\Desktop')
Excel = win32com.client.gencache.EnsureDispatch('Excel.Application') # Excel = win32com.client.Dispatch('Excel.Application')
win32c = win32com.client.constants
wb = Excel.Workbooks.Open('excel_file.xlsx')
Sheet1 = wb.Worksheets("Sheet1")
Sheet2 = wb.Worksheets("Sheet2")
xldata = Sheet1.UsedRange.Value
data_pivot = xldata[:24291]
cl1 = Sheet1.Cells(1,1)
cl2 = Sheet1.Cells(1+len(data_pivot)-1,1+len(data_pivot[0])-1)
PivotSourceRange = Sheet1.Range(cl1,cl2)
cl3=Sheet2.Cells(200,200)
PivotTargetRange= Sheet2.Range(cl3,cl3)
PivotTableName = 'ReportPivotTable'
PivotCache = wb.PivotCaches().Create(SourceType=win32c.xlDatabase, SourceData=PivotSourceRange, Version=win32c.xlPivotTableVersion14)
PivotTable = PivotCache.CreatePivotTable(TableDestination=PivotTargetRange, TableName=PivotTableName, DefaultVersion=win32c.xlPivotTableVersion14)
PivotTable.PivotFields('A').Orientation = win32c.xlRowField
PivotTable.PivotFields('A').Position = 1
PivotTable.PivotFields('B').Orientation = win32c.xlPageField
PivotTable.PivotFields('B').Position = 1
PivotTable.PivotFields('B').CurrentPage = 'b'
PivotTable.PivotFields('C').Orientation = win32c.xlPageField
PivotTable.PivotFields('C').Position = 2
PivotTable.PivotFields('C').CurrentPage = 5
PivotTable.PivotFields('D').Orientation = win32c.xlPageField
PivotTable.PivotFields('D').Position = 1
PivotTable.PivotFields('D').CurrentPage = 'd'
PivotTable.PivotFields('E').Orientation = win32c.xlPageField
PivotTable.PivotFields('E').Position = 3
PivotTable.PivotFields('E').CurrentPage = "(All)"
PivotTable.PivotFields('D').Orientation = win32c.xlColumnField
PivotTable.PivotFields('D').Position = 1
DataField = PivotTable.AddDataField(PivotTable.PivotFields('F'), Function = win32c.xlSum)
DataField = PivotTable.AddDataField(PivotTable.PivotFields('G'), Function = win32c.xlSum)
wb.SaveAs('excel_file.xlsx')
Excel.Application.Quit()
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.