python win32 COM关闭Excel工作簿

发布于 2024-11-15 01:23:19 字数 425 浏览 4 评论 0原文

我在 COM 中打开几个不同的工作簿(excel xlsx 格式),然后对它们进行处理。随着程序的进展,我希望关闭一本特定的工作簿,但保持其余工作簿打开。

如何关闭一本工作簿? (而不是整个 Excel 应用程序)

xl = Dispatch("Excel.Application")
xl.Visible = False
try:
    output = xl.Workbooks.Open(workbookName)
    output2 = xl.Workbooks.Open(workbook2Name)
except com_error:
    print "you screwed up blahblahblah"
    exit()

#work on some stuff
#close output but keep output2 open

I open several different workbooks (excel xlsx format) in COM, and mess with them. As the program progresses I wish to close one specific workbook but keep the rest open.

How do I close ONE workbook? (instead of the entire excel application)

xl = Dispatch("Excel.Application")
xl.Visible = False
try:
    output = xl.Workbooks.Open(workbookName)
    output2 = xl.Workbooks.Open(workbook2Name)
except com_error:
    print "you screwed up blahblahblah"
    exit()

#work on some stuff
#close output but keep output2 open

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

冷月断魂刀 2024-11-22 01:23:19

Workbook COM 对象有一个 Close() 方法。基本上,它应该是这样的:

xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open('New Workbook.xlsx')
# do some stuff
wb.Close(True) # save the workbook

上面只是一个框架,下面是一些在我的机器上针对 Office 2010 运行的代码:

from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add()
ws = wb.Worksheets.Add()
cell = ws.Cells(1)
cell.Value = 'Some text'
wb.Close(True, r'C:\Path\to\folder\Test.xlsx')

当然,这会创建一个新的 xlsx 文件。但随后我能够在同一会话中成功打开并修改文件,如下所示:

wb = xl.Workbooks.Open(r'C:\Path\to\folder\Test.xlsx')
ws = wb.Worksheets(1)
cell = ws.Cells(2)
cell.Value = 'Some more text'
wb.Close(True)

不知道这些是否有帮助......

The the Workbook COM object has a Close() method. Basically, it should be something like:

xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Open('New Workbook.xlsx')
# do some stuff
wb.Close(True) # save the workbook

The above was just a skeleton here's some code that works on my machine against Office 2010:

from win32com.client import Dispatch
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add()
ws = wb.Worksheets.Add()
cell = ws.Cells(1)
cell.Value = 'Some text'
wb.Close(True, r'C:\Path\to\folder\Test.xlsx')

Of course, that creates a new xlsx file. But then I'm able to successfully open and modify the file in the same session as follows:

wb = xl.Workbooks.Open(r'C:\Path\to\folder\Test.xlsx')
ws = wb.Worksheets(1)
cell = ws.Cells(2)
cell.Value = 'Some more text'
wb.Close(True)

Don't know if any of that helps...

轻许诺言 2024-11-22 01:23:19

您还可以尝试使用以下代码:

excel = Dispatch("Excel.Application")
excel.Visible = False
workbook = excel.Workbooks.Open(fileName)

# with saving
excel.DisplayAlerts = False
if saveAs:
    excel.ActiveWorkbook.SaveAs(fullFileNameToSave)
else:
    excel.ActiveWorkbook.Save()
excel.Quit()

#without saving

map(lambda book: book.Close(False), excel.Workbooks)
excel.Quit()

You can also try to use the following code:

excel = Dispatch("Excel.Application")
excel.Visible = False
workbook = excel.Workbooks.Open(fileName)

# with saving
excel.DisplayAlerts = False
if saveAs:
    excel.ActiveWorkbook.SaveAs(fullFileNameToSave)
else:
    excel.ActiveWorkbook.Save()
excel.Quit()

#without saving

map(lambda book: book.Close(False), excel.Workbooks)
excel.Quit()
时光与爱终年不遇 2024-11-22 01:23:19

此函数关闭所有打开的 Excel 文件

import os

def closeFile():

    try:
        os.system('TASKKILL /F /IM excel.exe')

    except Exception:
        print("KU")

closeFile()

This function closes any opened excel file

import os

def closeFile():

    try:
        os.system('TASKKILL /F /IM excel.exe')

    except Exception:
        print("KU")

closeFile()
心凉怎暖 2024-11-22 01:23:19
def setAutoFilter(self,ws,AmountToMatch):
        amounttopass = f"{AmountToMatch}"
        print("The Amount  of that month is  ::",amounttopass)
        ws.Range("B:G").AutoFilter(Field=6, Criteria1=amounttopass,VisibleDropDown=False)
        time.sleep(timeout)
def setAutoFilter(self,ws,AmountToMatch):
        amounttopass = f"{AmountToMatch}"
        print("The Amount  of that month is  ::",amounttopass)
        ws.Range("B:G").AutoFilter(Field=6, Criteria1=amounttopass,VisibleDropDown=False)
        time.sleep(timeout)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文