12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849 |
- '''Created 8 Aug 2023 Levi'''
- from datetime import datetime
- from time import (time, gmtime, strftime)
- import win32com.client
- wb_to_save = {'2020 havi forgalom.xlsx', 'HR_SYS_2021.xlsm', 'Finance_no_refresh.xlsm'}
- def time_it(func):
- def wrapper(*args, **kwargs):
- start = time()
- result = func(*args, **kwargs)
- end = time()
- mlsec = repr(end - start).split('.')[1][:3]
- elapsed = strftime(f"%M:%S.{mlsec}", gmtime(end - start))
- print(f'{func.__name__} finished in {elapsed}')
- return result
- return wrapper
- @time_it
- def save_all_open_workbooks():
- try:
- # Create a new instance of the Excel application
- excel_app = win32com.client.Dispatch('Excel.Application')
- # Set visible to False to hide Excel during the process
- excel_app.Visible = True
- # Get the Workbooks collection
- workbooks = excel_app.Workbooks
- # Save each workbook
- for wb in workbooks:
- if wb.Name in wb_to_save:
- wb.Save()
- print(f'saving {wb.Name}')
- print('All open workbooks saved successfully.', datetime.now().strftime('%d-%m-%Y %H:%M:%S'))
- except Exception as e:
- print(f'Error occurred: {e}')
- # finally:
- # # Quit Excel application
- # # excel_app.Quit()
- # pass
- if __name__ == '__main__':
- save_all_open_workbooks()
|