'Refresh Pivot Table without opening Excel wiht Python

i can refresh pivot tables with Python but i don't wanna to see excel, i wanna refresh as invisible on background.

import win32com.client

office = win32com.client.Dispatch("Excel.Application")
wb = office.Workbooks.Open(r"path\to\excel\file\to\print.xlsm")

count = wb.Sheets.Count
for i in range(count):
    ws = wb.Worksheets[i]

    pivotCount = ws.PivotTables().Count
    for j in range(1, pivotCount+1):
        ws.PivotTables(j).PivotCache().Refresh()
wb.Save()
wb.Close()

it didn't show up when I tried yesterday. but i am trying now, excel has been showing up, refresh and close. how can i refresh as invisible?



Solution 1:[1]

    xl = win32.gencache.EnsureDispatch("Excel.Application")
    xl.Visible = False
    xl.DisplayAlerts = False

Set Visible = False to let the refresh happen in the background, Excel will not be visible on the screen. You may also consider setting DisplayAlerts = False to suppress any alert window which require any user action.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 clover