I am using os, openpyxl and win32com.client in python to copy sheets from many files into 1. My code: '''
file_list = df['file_names'].unique().tolist()
#create a blank excel file
filepath = (report_path + '\\Combined Files.xlsx')
wb = openpyxl.Workbook()
wb.save(filepath)
# copy sheets in reverse order to new file
for s in (file_list[::-1]):
path1 = report_path + '\\' + s + '.xlsx'
path2 = filepath
xl = Dispatch("Excel.Application")
wb1 = xl.Workbooks.Open(Filename=path1)
wb2 = xl.Workbooks.Open(Filename=path2)
ws1 = wb1.Worksheets(1)
ws1.Copy(Before=wb2.Worksheets(1))
wb2.Close(SaveChanges=True)
xl.Quit()
# remove files
for f in file_list:
path = os.path.join(location, f)
os.remove(path)
print("%s has been removed successfully" %f)
'''
I get the following error when I run the code
os.remove(path) PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: file one name
I have done a search, but could not find any answers that related to my code - or that I could figure out how to use Here, Here, Here, and Here