Improved simulator

This commit is contained in:
Marius Ciepluch 2024-07-29 10:29:15 +02:00 committed by GitHub
parent a33706beea
commit 31c5d63710
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2,6 +2,33 @@ import os
import sys
import time
import pyautogui
import win32gui
import win32con
def close_cmd_window():
def enum_windows_callback(hwnd, result):
window_title = win32gui.GetWindowText(hwnd).lower()
if (win32gui.IsWindowVisible(hwnd) and
'cmd.exe' in window_title and
'Anaconda' not in window_title):
win32gui.PostMessage(hwnd, win32con.WM_CLOSE, 0, 0)
win32gui.EnumWindows(enum_windows_callback, None)
def close_excel_without_saving():
pyautogui.hotkey('alt', 'f4')
time.sleep(1)
# Try to locate and click the "Don't Save" button
try:
dont_save_button = pyautogui.locateOnScreen('dont_save_button.png', confidence=0.8)
if dont_save_button:
pyautogui.click(dont_save_button)
print("Clicked 'Don't Save' button")
else:
print("Save dialogue not found, Excel may have closed without prompting")
except pyautogui.ImageNotFoundException:
print("Save dialogue not found, Excel may have closed without prompting")
def open_excel_with_macros(file_path):
# Get the directory of the current script/executable
@ -18,10 +45,13 @@ def open_excel_with_macros(file_path):
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
try:
enable_button = pyautogui.locateCenterOnScreen(enable_button_image, confidence=0.8)
if enable_button:
pyautogui.click(enable_button)
break
except pyautogui.ImageNotFoundException:
pass
time.sleep(1) # Check every 1 second
if not enable_button:
@ -31,12 +61,13 @@ def open_excel_with_macros(file_path):
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
close_excel_without_saving()
# Close any cmd.exe windows that might have opened, except Anaconda prompt
close_cmd_window()
def main():
directory = r'C:\Users\mariu\Desktop\Corpus' # Adjust the path to your files
directory = r'C:\Users\student\Desktop\Corpus' # Adjust the path to your files
files = os.listdir(directory)
excel_files = [file for file in files if file.endswith(('.xlsx', '.xlsm'))]