Python的win32com没有关闭?

Python's win32com not closing?

我正在尝试关闭我使用 win32com 打开的 Excel sheet,但以下代码不起作用:

from win32com import DispatchEx
xlApp = client.DispatchEx("Excel.Application")
books = xlApp.Workbooks.Open(str(main_folder) + "\Original.xlsm")
ws = books.Worksheets["Sheet 1"]
ws.Visible = 1
ws.ExportAsFixedFormat(0, str(main_folder) + "\Duplicated")
ws.Close()

我收到以下错误:

"AttributeError: .Close"

我该如何解决?

如果你想关闭工作簿,你可以这样做:

from win32com import DispatchEx
xlApp = client.DispatchEx("Excel.Application")
books = xlApp.Workbooks.Open(str(main_folder) + "\Original.xlsm")
ws = books.Worksheets["Sheet 1"]
ws.Visible = 1
ws.ExportAsFixedFormat(0, str(main_folder) + "\Duplicated")
books.Close()  # changed line, use books instead of ws.

如果你有很多事情要做Excel,你也可以看看xlwings

Please use the below command to close the excel application.

xlApp.Quit()