通过纸条进行写操作后释放excel工作簿

2024-06-16 13:55:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用python脚本将数据写入excel工作簿。现在,我要释放指针。我不想因为其他原因而关闭工作簿。我只想松开指针。我在谷歌上搜索了一下,但没有找到答案。有人能帮忙吗?

from win32com.client import Dispatch xl = Dispatch('Excel.Application') xl.ScreenUpdating = True # performance xl.Visible = 1 xl.DisplayAlerts = True wbs = xl.Workbooks return xl, wbs

然后我用wb引用工作簿的不同工作表。
wb = xl.Workbooks.open(filename)


Tags: 数据答案from脚本clienttrue原因excel
1条回答
网友
1楼 · 发布于 2024-06-16 13:55:50

变量将在超出范围时释放。在下面的示例中,变量xl在退出get_workbook函数时被释放。如果需要显式删除变量,请使用del语句。你知道吗

from win32com.client import Dispatch

def get_workbook(path):
    xl = Dispatch('Excel.Application')
    xl.Visible = 1
    return xl.Workbooks.open(path)

wb = get_workbook(r'c:\temp\test.xlsx')
sheet = wb.Worksheets.Item(1)
sheet.Range('A1').Value2 = 'test'

del wb, sheet

相关问题 更多 >