简体   繁体   English

openpyxl,保存/使用后如何关闭/shutdown.xlsx 文件/会话?

[英]openpyxl, how to close/shutdown .xlsx file/session after saving/using it?

As I understood, there are no such functions: del , clear cache or garbage in openpyxl, maybe I am wrong.据我了解,openpyxl中没有这样的功能: del ,清除缓存或垃圾,也许我错了。 The problem I'm facing: The problem occurs when I want to save 2nd generated.xlsx file.我面临的问题:当我想保存第二个 generate.xlsx 文件时出现问题。 It somehow uses previous.xlsx file (and it's data) and can't merge some cells (from prev xlsx) since it does not have a write attribute.它以某种方式使用 previous.xlsx 文件(以及它的数据)并且无法合并某些单元格(来自 prev xlsx),因为它没有写入属性。

So how I can close/shutdown previous xlsx.那么我如何关闭/关闭以前的 xlsx。 I even tried remove xlsx file before generating new one and it also did not help我什至尝试在生成新文件之前删除 xlsx 文件,但它也没有帮助

code: (I dont have permission to post pics only links): pic 1: src(code, tree) how I am saving:代码:(我无权发布仅图片链接):图片 1: src(代码,树)我如何保存:

wb.save(filename=self.fileName)
wb.close()
  1. error错误

  2. class with id=1 xlsx class id=1 xlsx

  3. class with id=4 xlsx, and my code some how wrote data from prev one(for ex: ECOLOGY 17:00-19:00) it should not work like that class,id=4 xlsx,我的代码是如何从上一个写入数据的(例如:ECOLOGY 17:00-19:00)它不应该那样工作

  4. src code is here github src 代码在这里 github

I'm not suprised that you can't reuse the same workbook object to save a file with another name.对于您不能重复使用同一个工作簿 object 来保存具有其他名称的文件,我并不感到惊讶。 If you need to reuse this object, you can use workbook_obj.save(...);workbook_obj.close();workbook_obj=openpyxl.load_workbook(...) .如果需要重用这个 object,可以使用workbook_obj.save(...);workbook_obj.close();workbook_obj=openpyxl.load_workbook(...) I haven't tested this code, but it's my best guess.我没有测试过这段代码,但这是我最好的猜测。

I finally solved my problem.我终于解决了我的问题。 I followed @Michael Sohnen 's advice, But still, I'm not sure if openpyxl has a garbage-clearing feature or not.我听从了@Michael Sohnen 的建议,但是,我仍然不确定 openpyxl 是否具有垃圾清理功能。 but it doesn't matter但没关系
Solution: Instead of creating a new xlsx file, I used an existing file if it exists, otherwise I generated a new one.解决方案:我没有创建新的 xlsx 文件,而是使用现有文件(如果存在),否则我生成一个新文件。 That's all.就这样。
Explonation: As I understand it, he will download this particular file.xlsx, and openpyxl will know which file to work with, so we can be sure that we have avoided a data conflict.探索:据我了解,他会下载这个特定的文件.xlsx,openpyxl 会知道要使用哪个文件,所以我们可以确定我们已经避免了数据冲突。
final code:最终代码:

from openpyxl import Workbook, load_workbook
from django.conf import settings
from os import path, walk

class Base:

    def __init__(self,filename:str):
        self.fileName=filename
        self.filePath=path.join(settings.BASE_DIR,'xlsxFiles',filename)
        self.folderDir=path.join(settings.BASE_DIR,'xlsxFiles')
        self.workBook=None

    def getWorkSheet(self):
        files=[]
        for (dPath,dNames,dFiles) in walk(self.folderDir):
            files.extend(dFiles)
        if self.fileName in files:
            with open(self.filePath, 'rb') as xlsx:
                self.workBook=load_workbook(xlsx)
                workSheet=self.workBook[self.workBook.sheetnames[0]]
                return workSheet
        else:
            self.workBook=Workbook()
            workSheet=self.workBook.active
            return workSheet

    def saveXlsx(self):
        self.workBook.save(filename=self.filePath)
        self.workBook.close()

old one:老一:

class Base:
    wb=Workbook()
    sheet=wb.active

moreover I was saving generated.xlsx in in another code.py此外,我将 generated.xlsx 保存在另一个 code.py 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM