when I execute a an Xlwings function I can save and close the workbook. But I cannot close Excel 2016 anymore. Is this a known issue? How can I fix this?
5 Answers
Here is how I got it to work:
import xlwings as xw
wbPath = [WorkbookPath]
wb = xw.Book(wbPath)
app = xw.apps.active
wb.save(wbPath)
#wb.close()
app.quit()
Note that I commented out the line wb.close()
. You can skip this step and instead set the app = active Excel instance, save the workbook, and then quit the app.

- 1,409
- 5
- 20
- 44
Updated
You can use wb.app.quit()
when you want to close Excel and the associated workbook. Assume that wb
is your workbook. Keep in mind that wb.app.quit()
does't work if you used wb.close()
before wb.app.quit()
. Here's an example:
import xlwings as xw
path = r"test.xlsx"
wb = xw.Book(path)
wb.app.quit()
But also consider to open and close workbooks by using with xw.App() as app
(since version 0.24.3), I can only recommend it:
import xlwings as xw
with xw.App() as app:
wb = xw.Book("test.xlsx")
# Do some stuff e.g.
wb.sheets[0]["A1"].value = 12345
wb.save("test.xlsx")
wb.close()
The with
statement ensures proper acquisition and release of resources. The with
statement prevents, when an error occurs before closing Excel properly, the problem that Excel stays open and has possibly hidden excel processes left over in the background (because of xw.App(visible=False)
, if it is used). The with
statement has also the advantage that you don't need app.quit()
anymore, because Excel closes anyway at the end of the with block. But wb.close()
at the end of the with block is usable (but not necessary) – it achieves that the next time you open Excel, Excel will not display the message that Excel has recovered data that you may want to keep (as explained here).
As a side note, I had a situation where app.quit()
did not work. In this case I used app.kill()
instead.

- 1,457
- 12
- 20
-
1With xw.App(visible=False), xlwings creates a background process (in my task manager as "C:\Program Files\Microsoft Office\Root\Office16\EXCEL.EXE" /automation -Embedding). This doesn't always go away with `app.quit()`, but seems to so far with `app.kill()`. – voracity Jun 02 '21 at 04:01
Just to build on mouwsy's answer, I now have this context manager:
class XwApp(xw.App):
def __enter__(self, *args, **kwargs):
return super(*args, **kwargs)
def __exit__(self, *args):
for book in self.books:
try:
book.close()
except: pass
self.kill()
which I use like so:
with XwApp(visible=False) as app:
app.books.add()
# or
app.books.open('file.xlsx')
# ...
and this seems reasonably clean exiting so far. (But pre-opened Excel windows can always mess things up.)

- 811
- 1
- 10
- 5
-
This is related to: https://stackoverflow.com/questions/65189330/excel-exe-process-keeps-on-running-if-visible-false-after-any-error – mouwsy Jun 03 '21 at 12:02
I know this is old but I was unable to find an answer that worked and figured out a solve. I was able to close the instance of Excel by accessing the api property in xlwings.
xl = xw.apps.active.api
xl.Quit()
xlwings is just a fancy wrapper around pywin32, you can directly access the pywin32 functions by implementing the api property.

- 80
- 3
- 7
It's possible as follow. I comment that close the workbook you opened or created before.
import xlwings as xw
wb = xw.Book()
wb.close()
xw.App().quit()