[Python] Problem refreshing excel data from Pyhton Script [closed]

Discussão em 'Python' iniciado por Stack, Julho 30, 2025.

  1. Stack

    Stack Membro Participativo

    I have a python code that theoretically downloads data to an excel file and then uses the refresh all command for it to execute some Power Query on the downloaded data.

    import os
    import shutil
    import win32com.client

    staging_dir = r"C:\\XXX\\XXX\\XXX\\StagingFolder"
    if not os.path.exists(staging_dir):
    os.makedirs(staging_dir)

    destination_path_staging = os.path.join(staging_dir, destination_name)
    print(f"Temporary destination path: {destination_path_staging}")

    shutil.copy2(source, destination_path_staging)
    print(f"File copied to temporary folder: {destination_path_staging}")

    excel = win32com.client.Dispatch("Excel.Application")
    excel.Visible = False # Change to True if you want to see Excel
    print("Opening Excel file in temporary folder...")

    wb = excel.Workbooks.Open(destination_path_staging)
    print(f"File opened: {destination_path_staging}")

    wb.RefreshAll()
    print("Refreshing data...")

    excel.CalculateUntilAsyncQueriesDone()
    print("Data refreshed.")

    wb.Save()
    wb.Close()
    excel.Quit()
    print("Data update completed in temporary folder.")


    StagingFolder is considered trustworthy by Excel, I have configured it as such. Unfortunately, my code never reaches print("Data refreshed.").

    If I refresh manually the created excel file in StagingFolder, it just takes 5 minutes to refresh.

    Can someone please help me fix this?

    Continue reading...

Compartilhe esta Página