The following code selects the range of a pivot table and saves down as picture. How would it go to select the pivot table by its name (e.g., "Pivot1") and not by the range?
import win32com.client as win32
import sys
from pathlib import Path
import win32com.client as win32
from PIL import ImageGrab
excel_path = "C:/Prueba/GOOG-copia.xlsm"
excel = win32.DispatchEx('Excel.Application')
excel.Visible = False
excel.DisplayAlerts = False
wb = excel.Workbooks.Open(Filename=excel_path)
ws = wb.Worksheets('Cacaca')
win32c = win32.constants
ws.Range("A3:B8").CopyPicture(Format=win32c.xlBitmap)
img = ImageGrab.grabclipboard()
image_path = 'C:/Prueba/test.png'
img.save(image_path)
excel.Quit()
You can select named ranges by using the range method:
wb.Worksheets('Cacaca').Range("Pivot1").Select()
When using the Win32 library, you can often try the process with VBA first as the Win32 calls tend to map to VBA.
Update - Here is the code to list all pivot tables in a Workbook:
# create dictionary of Pivot tables by sheet
dd = {}
ShtCnt = excel.Sheets.Count
for s in range(ShtCnt):
#print("Sheet Name:", wb.Sheets(s+1).Name)
dd[wb.Sheets(s+1).Name] = []
cnt = wb.Sheets(s+1).PivotTables().Count
for x in range(cnt):
#print(wb.Sheets(s+1).PivotTables(x+1).Name)
dd[wb.Sheets(s+1).Name].append(wb.Sheets(s+1).PivotTables(x+1).Name)
print(dd)