The Olegrod
The Olegrod

Reputation: 21

How do I avoid crashes when using xlwings? It crashes on xw.App()

Im trying to make a simple sheet calculating the summation of two numbers: A3 = A1 + A2. I use the script below.

import xlwings as xw


app = xw.App(visible=False)

wb = xw.Book(r'C:/Data/xlwings/testxlwings.xlsx')
sh = wb.sheets["Sheet1"]
sh.range("A1").value = 683
sh.range("A2").value = 14
A3 = sh.range("A3").value
print(A3)
wb.save()
app.kill()

On one machine, this works perfectly and get the desired result of 697.0. On my other (virtual) machine I get the following:

Traceback (most recent call last):

  File "C:\Data\xlwings\xlwingssdemo.py", line 11, in <module>
    app = xw.App(visible=False)

  File "C:\Users\POuser1\Anaconda3\lib\site-packages\xlwings\main.py", line 204, in __init__
    self.impl = xlplatform.App(spec=spec, add_book=add_book)

  File "C:\Users\POuser1\Anaconda3\lib\site-packages\xlwings\_xlwindows.py", line 290, in __init__
    self._xl.Workbooks.Add()

  File "C:\Users\POuser1\Anaconda3\lib\site-packages\xlwings\_xlwindows.py", line 63, in __call__
    v = self.__method(*args, **kwargs)

  File "<COMObject <unknown>>", line 2, in Add

com_error: (-2146827284, 'OLE error 0x800a03ec', None, None)

I tried:

  1. reinstalling Python
  2. making sure all modules are same version
  3. trying every google link I could find

Thank you in advance,

Upvotes: 1

Views: 805

Answers (1)

mouwsy
mouwsy

Reputation: 1943

This is not really an answer to solve the problem, but new lines are not allowed in comments, therefore I write this as answer.
I would check if python has access to excel (by using the COM interface). Therefore, open a excel workbook on your virtual machine and execute the following code in python:

import win32com.client

excel = win32com.client.GetActiveObject("Excel.Application")
print(f"If a running Excel instance found, return the object:\n{excel}")

If this code runs without an error while excel is open, you know at least that python has access to excel.

Upvotes: 1

Related Questions