0

I need to open and edit my Excel with openpyxl, store the excel as a dataframe, and close the excel without any changes. Are there any ways to kill the excel and disable the auto-recovery dialogue which may pop out later?

The reason I'm asking is that my code worked perfectly fine in Pycharm, however after I packed it into .exe with pyinstaller, the code stopped working, the error said "Excel cannot access the file, there are serval possible reasons, the file name or path does not exist, or the file is being used by another program, or the workbook you are saving has the same name as a currently open workbook.

I assume it is because the openpyxl did not really close the excel, and I exported it to a different folder with the same file name.

Here is my code:

wb1 = openpyxl.load_workbook(my_path, keep_vba=True)
ws1 = wb1["sheet name"]
making changes...
ws1_df = pd.DataFrame(ws1.values)
wb1.close()

Many thanks ahead :)

devBear
  • 13
  • 1

1 Answers1

-1

The following way you can do this. solution

from win32com.client import Dispatch

# Start excel application
xl = Dispatch('Excel.Application')

# Open existing excel file
book = xl.Workbooks.Open('workbook.xlsx')

# Some arbitrary excel operations ...    

# Close excel application without saving file
book.Close(SaveChanges=False)
xl.Quit()

  • Hi, thanks for answering. Will this be conflicted with openpyxl? Do I do the openpyxl open, edit, close operations in # Some arbitrary excel operations ... section? Thanks – devBear Apr 12 '22 at 12:11
  • Hi, go through this thread https://stackoverflow.com/questions/31416842/openpyxl-does-not-close-excel-workbook-in-read-only-mode – Lean Learner Apr 12 '22 at 12:16
  • An answer that links to another answer, isn't an answer. In any case, the question here was about openpyxl. – Charlie Clark Apr 12 '22 at 16:51