Search code examples
python-3.xpython-multithreadingxlwings

Thread that calls a xlwings function returns pywintype error


I am trying to make it where my GUI does not freeze when I run a function, so I put it into a thread, however when I use it as a thread, I get the following error;

Error:

  • pywintypes.com_error: (-2147417842, 'The application called an interface that was marshalled for a different thread.', None, None)

I am currently using xlwings to do something with an excel sheet in the SaveRunThread function

import xlwings as xw

 def SaveRun(self):
        try:
            saverun = threading.Thread(target=self.SaveRunThread)
            saverun.start()
        except:
            print('Not Run')

 def SaveRunThread(self):
        'Save Run File to directory'
        try:
            app = xw.App(visible=False)

            book = xw.Book(r'{0}\Template_VS.xlsx'.format(TemplateDirectory[-1]))
              ### Do something

            book.save(r'{0}\{1}.xlsx'.format(newDirectory[-1], nameList[-1]))
            app.kill() #####Error occurs here
       except:
           print('Not Run')

I'd like for it to run the code when called without freezing the GUI. I don't have a MCVE right now, so I just posted partial code.


Solution

  • I created a workaround for this error. I created my own thread class, and put the code where I did something with xlwings in the run function and use signals to communicate with main thread.

    The error occurred when I used app.kill() in the thread.

    In my GUI/ui, I created an empty list before thread started, appended the app method, then accessed item in list to kill the excel when thread was finished. This worked for me.

    ######This is the code in my ui init method######
        self.saveThread = SaveThread()
        self.saveThread.finished.connect(self.on_finished)
    
    
        def SaveRun(self):
            'Save Run File to directory'
            try:
                directory = str(QFileDialog.getExistingDirectory(self, "Select Directory"))
                saveDirectory.append(directory)
                app = xw.App(visible=False)
                appList.append(app)
                self.saveThread.start()
    
        def on_finished(self):
                appList[-1].kill()
    
    class SaveThread(QtCore.QThread):
        saveSignal = pyqtSignal()
        def __init__(self, parent = None):
            super().__init__(parent)
    
        def run(self):
            book = xw.Book(r'{0}\Template_VS.xlsx'.format(TemplateDirectory[-1]))
               ### Do something with excel
            book.save(r'{0}\{1}.xlsx'.format(newDirectory[-1], nameList[-1]))
            self.saveSignal.emit()