Closing files in openpyxl

None of these processes, as expected, will read the documentation:

worksheet.close()
workbook.close()

Is there a way to close files once made in openpyxl? Or is it processed automatically when the program exits? I do not want to leave spreadsheets left in my memory.

+5
source share
1 answer

Ok, you can take a look at the source code, Im currently using 1.5.5 as such

def load_workbook(filename, use_iterators=False):        
    if isinstance(filename, file):
        # fileobject must have been opened with 'rb' flag
        # it is required by zipfile
        if 'b' not in filename.mode:
            raise OpenModeError("File-object must be opened in binary mode")

    try:
        archive = ZipFile(filename, 'r', ZIP_DEFLATED)
    except (BadZipfile, RuntimeError, IOError, ValueError), e:
        raise InvalidFileException(unicode(e))
    wb = Workbook()

    if use_iterators:
        wb._set_optimized_read()

    try:
        _load_workbook(wb, archive, filename, use_iterators)
    except KeyError, e:
        raise InvalidFileException(unicode(e))
    finally:
        archive.close()
    return wb

it looks like it closes the archive when we download the book, how about saving it?

  def save(self, filename):
    """Write data into the archive."""
    archive = ZipFile(filename, 'w', ZIP_DEFLATED)
    self.write_data(archive)
    archive.close()

it looks like it also closes the archive when we save it.

Excel , , , , , , .

, - openpyxl? , ? , .

, wb.save(filename = dest_filename), handled automatically , , , openpyxl , no class Workbook(object): __del__, , , 1.5.5 1.5.8, , , .

+10

All Articles