1

I have searched and cannot seem to find the answer to my issue. I’m hoping someone can help.

Here is a slimmed down version of my code. I have it within a unit test TestCase class. I open three books, one of which is an xlsm file (wb2), do some processing, and then save the wb2 file with another name and close all three workbooks.

What's happening is that all three workbooks are closing, but there still remains an instance of Excel open. Like, an empty shell with nothing in it. I searched and found the kill() method, but that is not killing it (whether I use self.xw.App().kill() or just xw.App().kill()). Also, I print out all of the open apps and it returns an empty list. So, I don't know what this instance remaining is all about and why it's not getting killed. BTW, I am using Excel 365. Thanks in advance.

import xlwings as xw

class TestClass(unittest.TestCase):

    def test_xltest(self):     

        #Open 3 separate workbooks
        self.wb1 = xw.Book(workbook1)
        self.wb2 = xw.Book(workbook2)
        self.wb3 = xw.Book(workbook3)

        #Do some processing here

    def tearDown(self):            
        self.wb1.close()
        self.wb2.save(self.newDirectoryPath)
        self.wb2.close()
        self.wb3.close()

        print (xw.apps)
        self.xw.App().kill()

if __name__ == "__main__":
    unittest.main()
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
SAB15
  • 41
  • 1
  • 4
  • xlwings currently needs at least 1 workbook open to be able to talk to the application instance. So quit or kill your app before you close the last workbook. – Felix Zumstein Mar 28 '20 at 23:10
  • 1
    Thank you. I found this solution on another post and it worked like a charm. app = xw.apps.active app.quit() – SAB15 Mar 29 '20 at 19:48
  • See also [with xw.App() as app:](https://stackoverflow.com/a/70158361/13968392), which is in my opinion the simplest approach. – mouwsy Feb 16 '23 at 21:17

0 Answers0