Simulator code to auto-accept Excel Macros

This commit is contained in:
marius 2024-06-23 20:07:12 +02:00
parent 2a0ad000ae
commit 24886d88e7
2 changed files with 49 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 891 B

View File

@ -0,0 +1,49 @@
import os
import sys
import time
import pyautogui
def open_excel_with_macros(file_path):
# Get the directory of the current script/executable
base_path = getattr(sys, '_MEIPASS', os.path.dirname(os.path.abspath(__file__)))
enable_button_image = os.path.join(base_path, 'enable_content.png')
# Open Excel through the OS start menu or command line
os.startfile(file_path)
time.sleep(5) # Wait for Excel to open
# Custom timeout mechanism to locate the 'Enable Content' button
timeout = 10 # 10 seconds timeout
start_time = time.time()
enable_button = None
while (time.time() - start_time) < timeout:
enable_button = pyautogui.locateCenterOnScreen(enable_button_image, confidence=0.8)
if enable_button:
pyautogui.click(enable_button)
break
time.sleep(1) # Check every 1 second
if not enable_button:
print("Enable Content button not found, continuing...")
# Wait for any macros to finish running or other processing
time.sleep(10) # Adjust time based on expected macro execution time
# Close Excel without saving
pyautogui.hotkey('alt', 'f4')
time.sleep(1)
pyautogui.press('n') # Press 'n' in response to Excel's save prompt
def main():
directory = r'C:\Users\mariu\Desktop\Corpus' # Adjust the path to your files
files = os.listdir(directory)
excel_files = [file for file in files if file.endswith(('.xlsx', '.xlsm'))]
for file in excel_files:
full_path = os.path.join(directory, file)
open_excel_with_macros(full_path)
time.sleep(5) # Adjust as needed between openings
if __name__ == '__main__':
main()