I'm trying to close an Excel sheet that I have open using win32com, but the following code doesn't work:
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()
I get the following error:
"AttributeError: .Close"
How should I solve this?
If you want to close the workook, you can do:
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.
You can also see xlwings if you have lots of things to do with Excel.