0

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?

2

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

-1

Please use the below command to close the excel application.

xlApp.Quit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.