I've been trying to overwrite an Excel file after performing some workbook operations using openpyxl. Due to access permissions being required to use the file in question, smbclient is being employed to access it.
The problem arises when trying to save the new version of the file using the built-in save()
method for openpyxl objects, as according to the docs, the save()
method involves opening the file to complete the operation:
>>> excel_file.save('\path\to\my\file.xlsx')
PermissionError: [Errno 13] Permission denied: '\path\to\my\file.xlsx'
As the file is restricted, the operation needs to be performed inside a with
block where the smbclient is opening the file. However, this results in the same error being thrown as the save()
method can no longer open the file to complete the operation as the file has already been opened using smbclient (the file is blocked):
with smbclient.open_file('\path\to\my\file.xlsx', mode='rb') as s:
xlsx_file.save(s.name)
PermissionError: [Errno 13] Permission denied: '\path\to\my\file.xlsx'
Here, s.name
just gets the path attribute of the open file object.
Does anyone know how I get around this problem?
I working with this connection and this works for me: