On 27/01/2013 12:37, Hsu. Victor (GSM) wrote: > Hi, > > I would like to copy a Pivot table to another location. > I can do that with VBA code below. Can someone guide me how to convert > below code to Python? Especailly the PivotTables PivotSelect part, I > don't know how to call it in python. > > Sub copyPivot() > Dim ws As Worksheet > Set ws = Worksheets.Add > Sheets("PV1").Select > ActiveSheet.PivotTables("Pivot1").PivotSelect "", xlDataAndLabel, True > Selection.Copy > ws.Range("A1").PasteSpecial > End Sub
I don't have a sheet with a pivot table (and I can't be bothered to generate one :) ) but hopefully the code below will show you enough of the pattern to get you going. It's untested so there may be some slight flaws: <code> import win32com.client const = win32com.client.constants wb = win32com.client.GetObject(r"c:\path\to\spreadshset.xls") ws = wb.Worksheets.Add() wb.Sheets("PV1").Select() wb.ActiveSheet.PivotTables("Pivot1").PivotSelect( "", const.xlDataAndLabel, True) wb.Application.Selection.Copy() ws.Range("A1").PastSpecial() </code> TJG _______________________________________________ python-win32 mailing list python-win32@python.org http://mail.python.org/mailman/listinfo/python-win32