OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Openpyxl method workbook.close() not working

  • Thread starter Thread starter Brianna D
  • Start date Start date
B

Brianna D

Guest
I am trying to simply write to a spreadsheet and save it. Later on in the code the spreadsheet is being moved with shutil.move, so the file needs to be closed when it gets to that point or else shutil is unable to delete the old file. However, calling wb.close() is not actually closing the workbook, and throwing an access error when trying to delete the file later.

Here is the method to write to, save, and close the file:

Code:
def savetocell(r,col,val,file,sheet):
    from openpyxl import load_workbook
    book = load_workbook(file)
    page = book[sheet]
    page.cell(row = r,column=col+1,value=val)
    book.save(file)
    book.close()

I've been at this issue for a while and I've tried using read and write only modes, switching to pandas and xlwings (I would prefer to stick with openpyxl for formatting ease with the rest of the library), and pretty much every other trick I saw online (please recommend anything though, third times a charm!). Regardless the file still won't close. The issue seems to be that the workbook is missing the attribute archive after looking into the openpyxl documentation, but I am really not sure how I am to change that. Thank you in advance!

Code:
    def close(self):
        """
        Close workbook file if open. Only affects read-only and write-only modes.
        """
        if hasattr(self, '_archive'):
            self._archive.close()

EDIT: I am running python 3.12.4 with openpyxl 3.1.2, and I am working with xlsx files.
<p>I am trying to simply write to a spreadsheet and save it. Later on in the code the spreadsheet is being moved with shutil.move, so the file needs to be closed when it gets to that point or else shutil is unable to delete the old file. However, calling wb.close() is not actually closing the workbook, and throwing an access error when trying to delete the file later.</p>
<p>Here is the method to write to, save, and close the file:</p>
<pre><code>def savetocell(r,col,val,file,sheet):
from openpyxl import load_workbook
book = load_workbook(file)
page = book[sheet]
page.cell(row = r,column=col+1,value=val)
book.save(file)
book.close()
</code></pre>
<p>I've been at this issue for a while and I've tried using read and write only modes, switching to pandas and xlwings (I would prefer to stick with openpyxl for formatting ease with the rest of the library), and pretty much every other trick I saw online (please recommend anything though, third times a charm!). Regardless the file still won't close. The issue seems to be that the workbook is missing the attribute archive after looking into the openpyxl documentation, but I am really not sure how I am to change that. Thank you in advance!</p>
<pre><code> def close(self):
"""
Close workbook file if open. Only affects read-only and write-only modes.
"""
if hasattr(self, '_archive'):
self._archive.close()
</code></pre>
<p>EDIT: I am running python 3.12.4 with openpyxl 3.1.2, and I am working with xlsx files.</p>
 

Latest posts

H
Replies
0
Views
1
Hür Doğan ÜNLÜ
H
Top