本质上它是你的类对象在内存中持久化。考虑使用with()在上下文管理器中包装进程。并在上下文中调用invoice_make()。在
此外,您的Excel方法不正确,它用方括号将工作簿索引为零。在
最后,考虑使用os.path.join()来避免反斜杠或正斜杠,并使用try/except块来捕获COM异常并从内存中正确释放对象。在import openpyxl as xl
import os
from win32com import client
cwd = os.getcwd()
class CTAutomation:
def __init__(self):
self.invoice = xl.load_workbook(os.path.join(cwd, "Templates", "ctrates.xlsx"))
self.xlTemplate = xl.load_workbook(os.path.join(cwd, "Templates", "invoiceTemplate.xlsx"))
self.vpc = xl.load_workbook(os.path.join(cwd, "Templates", "Vpc.xlsx"))
def invoice_make(self, file):
try:
self.xlApp = client.Dispatch("Excel.Application")
self.xlbook = self.xlApp.Workbooks.Open(os.path.join(cwd, "TestFiles", file))
self.ws = self.xlbook