I am using Notebooks in ArcGIS Pro (v3.3) to execute a python script. The script downloads Excel data from a hosted feature service. Using the openpyxl library, the script opens an empty excel template and transfers data into the template. Then the script saves the template, which now contains data, as a separate file. The final step is to remove the template, and at this step, I get an error message saying:
“[WinError 32] The process cannot access the file because it is being used by another process”.
###
excel1 = openpyxl.load_workbook(file path to xlsx document) ## holds source data that will be copied
excel2 = openpyxl.load_workbook(file path to xlsx document) ## blank template
### variables are defined based on values in cells in excel1 and passed into cells in excel2 like this:
value1 = excel1.cell(1.1).value
excel2.cell(2.2.).value = value1
###save the template with values as a new Excel file
excel2.save(file path)
### remove the original template
excel2.close()
os.remove(file path to excel2)
##### I also tried this approach and got the same message at the remove step:
excel2.save(path to new file)
excel2filepath = (file path the xlsx doc)
excel2.close
os.remove(excel2filepath)
Does anyone know why the xlxs file is locked? How can I remove the file?
This script worked until recently, and none of the code was changed. Is there an issue with Excel that could be causing this?
I was expecting the script to delete the template (excel2) after it was saved as a new file. This script was working fine until recently. None of the code was changed. This is the error message I received:
PermissionError Traceback (most recent call last)
In [5]:
Line 227: transferToMT()
In [5]:
Line 216: os.remove(os.path.join(CP_Folder, ProjectName + “_MTBlank.xlsx”))
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: ‘C:FNL_ProjectsDocumentationAndrewTest4AndrewTest4_CPAndrewTest4_MTBlank.xlsx
Andrew is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.