When I run a Pythonscript I get the PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: (filename)
First I run a SQL-script and write the result to excel. Then I want to convert the xslx-file to a xlsb-file and remove the xslx.
My code after the to_excel and writer.save:
naam = Export_locatie + 'Map' + "\\" +'Name' + str(date.today())
try:
os.remove(naam + '.xlsb')
except:
print("file not available")
excel = win32.Dispatch('Excel.Application')
wb = excel.Workbooks.Open(Bestands_naam)
#Activate second sheet
excel.Worksheets(1).Activate()
#Autofit column in active sheet
excel.ActiveSheet.Columns.AutoFit()
excel.ActiveSheet.Columns.AutoFilter()
wb.SaveAs(naam + '.xlsb', 50 )
wb.Close()
os.remove(naam + '.xlsx')
All my colleagues can run the script without error. Is it possible to find using Python where the file is in use? I think it is because of a setting in my os.