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
看起来是的,它确实关闭了存档,当我们加载工作簿时,保存时如何?
def save(self, filename):
"""Write data into the archive."""
archive = ZipFile(filename, 'w', ZIP_DEFLATED)
self.write_data(archive)
archive.close()
Is there a way to close files once done in openpyxl? Or is it handled automatically when the program quits? I dont want to leave spreadsheets left hanging in memory.
好吧,你可以看看源代码,Im目前使用的是1.5.5版本
看起来是的,它确实关闭了存档,当我们加载工作簿时,保存时如何?
看起来我们保存时它也会关闭存档。
从根本上讲,我们从一个随后关闭的文件中将excel工作簿读入内存,进行更新,如果不保存它,更改可能会丢失,如果保存它,则在写入后关闭该文件。
您可以使用
wb.save(filename = dest_filename)
保存您的更改,例如handled automatically
在读取或写入文件时,在操作后关闭它,但如果openpyxl自动保存您的更改,则class Workbook(object):
没有__del__
则在删除或垃圾收集该对象时不会调用任何内容,同样,这是为了1.5.5
当前版本是1.5.8
截至本文撰写之时,我怀疑已经发生了很大的变化。相关问题 更多 >
编程相关推荐