I have a python code, that consists of an basic User Interface, in this UI the User select a language (used in the SAP) and the user can select a machine serial number.
When the user press the Run button, the code will run a SAP script, this script export the bill of materials (BOM) of the selected machine.
Until this part I am having no problems, however when you export the BOM out of SAP it automatically opens it in a Excel file; but to calculate the area, I need to read the excel, so..... I have to somehow, automatically, close the excel that was opened by the SAP.
Currently I am making my program to press "alt" + "f4", and it works fine if the user does not press any buttons while the program is running.
So, my question is, is it possible to make the python code close an expecific tab of the Excel (assume I have more than 1 workbook open).
I tried to use the xlsxwriter to close the workbook, however in my research I only manage to know how to use the .close function of an workbook that I made using the library, not an already opened workbook.
I tought that i could use the option using the os library to close the EXCEL.EXE, however this does not work in my case, cause in our workspace here at my work, people always have the excel application open; so i cannot close all the workbooks.
The "alt" + "f4" was a solution that chat GPT brought me, and i was desperate at the time, so i tought of using it, and it works like a charm (if the user does not press any mouse/keyboard key).
The best solution is to close the specific workbook that SAP opned; however if this is not possible, i want to know if it is possible to "blocK" any user inputs, while the program runs, so we do not have any execution erros.
UPDATE 02/02/2024
I was able to solve the problem; the code part used was the following:
Excel = win32com.client.Dispatch("Excel.Application") wb = Excel.Workbooks.Open(file_complete_path_pintura) wb.Close(False) time.sleep(1)
Now it works perfectly; the SAP opens the Excel, but using the win32com I set the opended Excel as wb; and then using the Close function, I am able to close the Excel and read the exported data.