2

xlwings works fine on my computer, but when I try to transfer the same set up to another computer it seems to not open correctly giving me this error

Traceback (most recent call last):

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 432, in __call__

    return Book(xl=self.xl(name_or_index))

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 152, in __call__

    v = self._inner(*args, **kwargs)

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\win32com\client\dynamic.py", line 197, in __call__

    return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.defaultDispatchName,None)

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147352565), None)



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 2776, in open

    impl = self.impl(name)

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 434, in __call__

    raise KeyError(name_or_index)

KeyError: 'output2019-06-03.11-40timeseries_5-31-2019scrubbed.xlsx'



During handling of the above exception, another exception occurred:



Traceback (most recent call last):

  File "Computation.py", line 157, in <module>

    xwWb = xw.Book("output" + timeName + os.path.split(file_path)[1])

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 488, in __init__

    impl = app.books.open(fullname).impl

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\main.py", line 2787, in open

    impl = self.impl.open(fullname)

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 443, in open

    return Book(xl=self.xl.Open(fullname))

  File "C:\Users\base7268\AppData\Local\Programs\Python\Python37\lib\site-packages\xlwings\_xlwindows.py", line 63, in __call__

    v = self.__method(*args, **kwargs)

  File "<COMObject <unknown>>", line 8, in Open

pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Microsoft Excel', 'Open method of Workbooks class failed', 'xlmain11.chm', 0, -2146827284), None)

Here is some simplified code where none of the data changing is occurring. Using ospath absolute does not help with the error. Both systems run the same 64 bit operating system and 32 bit excel. Yet the error persists on the second machine. There is no real difference I can understand that would give me an error on the 2nd system over the first one. Both machines are PCs

import xlrd
import openpyxl
import xlwings as xw
from xlwings import constants
import os
import tkinter as tk
from tkinter import filedialog

import datetime


root = tk.Tk()
root.withdraw()

file_path = filedialog.askopenfilename()
print(file_path)

start = time.time()


wb = openpyxl.load_workbook(file_path)
Returns = wb['Prices']
newWs = wb.create_sheet()
newWs.title = "NominalDailyReturns"

benchWS = wb.create_sheet()
benchWS.title = "ActiveDailyReturns"


thirdWs = wb.create_sheet()
thirdWs.title = "RawAnalysis"
thirdWs.column_dimensions["A"].width = 32

name4 = thirdWs.title


print("halfway")

print(os.path.split(file_path))
print("output" + timeName + os.path.split(file_path)[1])

wb.save("output" + timeName + os.path.split(file_path)[1])

xwWb = xw.Book(os.path.abspath("output" + timeName + os.path.split(file_path)[1]))
XnewWs = xwWb.sheets['NominalDailyReturns']

xwWb.save()
xwWb.close()

wb = openpyxl.load_workbook("output" + timeName + os.path.split(file_path)[1])
benchWS = wb['ActiveDailyReturns']

wb.save("output" + timeName + os.path.split(file_path)[1])
xwWb = xw.Book("output" + timeName + os.path.split(file_path)[1])

XthirdWs = xwWb.sheets['RawAnalysis']
xwWb.save()
xwWb.close()
chubbygoat
  • 31
  • 1
  • 5
  • Could you please provide a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example), or the code for the error section? Maybe you should use `os.path.abspath()` to convert file system paths to absolute: https://stackoverflow.com/questions/5879754/run-excel-file-from-python-error. – Drake Wu Jun 04 '19 at 05:18
  • I tried os path absolute, did not work on opening the first time. Did it with both opens, also did not work. I edited in a minimal reproducible example above. Let me know if you have any suspicions. The 'Open method of Workbooks class failed' error is relatively new and I can't find it anywhere online. – chubbygoat Jun 04 '19 at 18:49

1 Answers1

1

I figured out the problem with my code. When I used openpyxl to edit my excel data, one of the formulas was subtracting a word from a number, raising an error and corrupting the excel file. Then xlwings tried to open a corrupted file, and it failed on me.

So the solution is to not save a corrupted excel file and expect xlwings to open it.

chubbygoat
  • 31
  • 1
  • 5