import win32com.
client
import pythoncom
import gc
import time
from constants import ListFilePath
def refresh_excel():
"""Refreshes the Excel file and updates calculations."""
excel_path = ListFilePath
try:
pythoncom.CoInitialize() # Initialize COM threading
start_time = time.time() # Start timer
# Open Excel (hidden mode)
excel = win32com.client.DispatchEx("Excel.Application")
excel.Visible = False
excel.ScreenUpdating = False
excel.DisplayAlerts = False
# Open workbook and first sheet (change index if needed)
wb = excel.Workbooks.Open(excel_path)
ws = wb.Sheets(1) # Change to specific sheet if needed (e.g., ws =
wb.Sheets["Sheet1"])
# Refresh only the specific range
ws.Range("AA1:AB10").Calculate()
# Save and close
wb.Save()
wb.Close(SaveChanges=True)
# Quit Excel properly
excel.Quit()
# Release COM object and clean memory
del ws
del wb
del excel
gc.collect()
end_time = time.time() # End timer
elapsed_time = end_time - start_time
print(f"Excel range AA1:AB10 refreshed successfully in {elapsed_time:.2f}
seconds.")
except Exception as e:
print(f"Error refreshing Excel range: {e}")