1

I am opening an Excel with openpyxl library, however it is saving the Excel in read - only mode

I tried the answer of this question:

Openpyxl does not close Excel workbook in read only mode

wb._archive.close()

but it give me AttributeError: 'Workbook' object has no attribute '_archive'.

I am working with the following code:

wb = openpyxl.Workbook()
sheet1 = wb.create_sheet("mysheet", 0)
sheet1 = wb["mysheet"]   
sheet1.cell(row=1, column=1).value = '123'
sheet1.cell(row=1, column=2).value = 'summary'
wb.save(filename) /*filename has the adress of xlsx*/

The Excel file is created without problem, however it is in 'read only' mode. How can I prevent this? Is there an option for the save or create method to avoid read only mode?

Thanks for your help.

Larx
  • 161
  • 1
  • 4
  • 16
  • What do you mean by read-only mode? – Charlie Clark Jan 08 '19 at 10:14
  • When I open the Excel is in read only mode. – Larx Jan 08 '19 at 10:19
  • read-only means your file is being written while you are trying to open. so you need to close it. in your code like `wb._archive.close() ` – Nihal Jan 08 '19 at 10:22
  • This solution does not work for me. It gives me: AttributeError: 'Workbook' object has no attribute '_archive'. – Larx Jan 08 '19 at 10:34
  • then try only `wb.close()` – Nihal Jan 08 '19 at 10:43
  • I add wb.close() after save, but file is still in read only mode. – Larx Jan 08 '19 at 10:52
  • I suspect the problem is simply one of file permissions in Windows. Read-only mode in openpyxl have nothing to do with this and is not relevant here. Check the permissions and ownership of the file you've created and that it is not opened by any other program or process. – Charlie Clark Jan 08 '19 at 11:05
  • Indeed, it was Windows permissions, because I was save it in a folder in C:\. Please @CharlieClark post your comment as an answer to mark as correct for my case. – Larx Jan 08 '19 at 11:25

1 Answers1

1

I suspect the problem is simply one of file permissions in Windows. Read-only mode in openpyxl have nothing to do with this and is not relevant here. Check the permissions and ownership of the file you've created and that it is not opened by any other program or process.

Charlie Clark
  • 18,477
  • 4
  • 49
  • 55