| 1 |
nino.borges |
953 |
"""
|
| 2 |
|
|
|
| 3 |
|
|
ExcelRuningConnector
|
| 4 |
|
|
|
| 5 |
|
|
Created by:
|
| 6 |
|
|
Emanuel Borges
|
| 7 |
|
|
07.25.2025
|
| 8 |
|
|
|
| 9 |
|
|
A library for connecting to an already running Excel instance.
|
| 10 |
|
|
|
| 11 |
|
|
"""
|
| 12 |
|
|
|
| 13 |
|
|
|
| 14 |
|
|
import time
|
| 15 |
|
|
|
| 16 |
|
|
from win32com.client import GetActiveObject
|
| 17 |
|
|
|
| 18 |
|
|
|
| 19 |
|
|
def get_excel_app_with_retry(retries=5, delay=1):
|
| 20 |
|
|
for attempt in range(retries):
|
| 21 |
|
|
try:
|
| 22 |
|
|
xlApp = GetActiveObject("Excel.Application")
|
| 23 |
|
|
return xlApp
|
| 24 |
|
|
except Exception as e:
|
| 25 |
|
|
print(f"Attempt {attempt+1}: Excel not ready. Retrying in {delay}s...")
|
| 26 |
|
|
time.sleep(delay)
|
| 27 |
|
|
raise RuntimeError("Failed to connect to Excel after multiple attempts.") |