2

Here is my logic that I am trying to understand.

import xlwings as xw
import threading
workbook = ""

def function1():
    t1 = threading.Thread(target=function2)
    t1.start()
    t1.join()

def function2():
    global workbook
    workbook = xw.Book()

function1()
print (workbook)

When I run the following I am receiving the following message:

Traceback (most recent call last):
  File "c:\Users\rakesh.ramanjulu\Documents\Python Projects\Vendor ScoreCard Accuracy Final\test.py", line 51, in <module>
    print (workbook)
  File "C:\Users\rakesh.ramanjulu\AppData\Local\Programs\Python\Python312\Lib\site-packages\xlwings\main.py", line 1288, in __repr__
    return "<Book [{0}]>".format(self.name)
                                 ^^^^^^^^^
  File "C:\Users\rakesh.ramanjulu\AppData\Local\Programs\Python\Python312\Lib\site-packages\xlwings\main.py", line 1105, in name
    return self.impl.name
           ^^^^^^^^^^^^^^
  File "C:\Users\rakesh.ramanjulu\AppData\Local\Programs\Python\Python312\Lib\site-packages\xlwings\_xlwindows.py", line 837, in name
    return self.xl.Name
           ^^^^^^^^^^^^
  File "C:\Users\rakesh.ramanjulu\AppData\Local\Programs\Python\Python312\Lib\site-packages\xlwings\_xlwindows.py", line 199, in __getattr__
    v = getattr(self._inner, item)
        ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\rakesh.ramanjulu\AppData\Local\Programs\Python\Python312\Lib\site-packages\xlwings\_win32patch.py", line 55, in __getattr__
    return getattr(d, attr)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\rakesh.ramanjulu\AppData\Local\Programs\Python\Python312\Lib\site-packages\win32com\client\__init__.py", line 585, in __getattr__
    return self._ApplyTypes_(*args)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\rakesh.ramanjulu\AppData\Local\Programs\Python\Python312\Lib\site-packages\win32com\client\__init__.py", line 574, in _ApplyTypes_
    self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
pywintypes.com_error: (-2147220995, 'Object is not connected to server', None, None)

I am slightly confused as to why this is happening. Updating a global variable via threads has never caused me issues, but not sure hot to address the above situation. I would assume that the variable would change, but nope.

2
  • I got this error when my excel app is not started. If I start an Excel app, then running your codes works fine for me.
    – rachel
    Commented Jun 30 at 4:16
  • I would have expected you to have this issue
    – moken
    Commented Jul 1 at 9:48

1 Answer 1

0

To elaborate what happens on my mac:

If I allow xw.Book() to launch the Excel app, I got

enter image description here

If I start Excel (not creating a workbook, just start the app) before hand, your code works fine: enter image description here

enter image description here

Not the answer you're looking for? Browse other questions tagged or ask your own question.