I am trying to run an Excel function through Python using pywin32. The function is a Reuters Excel add-on function (for, those unfamiliar, Reuters is a financial data provider).
from win32com import client
from paths import path_onedrive
import datetime as dt
today = str(dt.datetime.today().date()).replace('-','')
xlApp2 = client.Dispatch('Excel.Application')
xlApp2.Visible = True
wb = xlApp2.Workbooks.Add()
wb.SaveAs(fr'{path_onedrive}\reuters\{today}.xlsx')
ws_sheet1 = wb.Worksheets('Sheet1')
ws_sheet1.Cells(1, 'A').Value = '=TR("USDSROIS=";"BID;ASK;TRDPRC_1;HST_CLOSE";"CH=Fd RH=IN";B2)'
Where, =TR("USDSROIS=";"BID;ASK;TRDPRC_1;HST_CLOSE";"CH=Fd RH=IN";B2) is the Excel function that needs to be ran. The error I get is:
Traceback (most recent call last):
File "C:\Users\Sergej Shteriev\IdeaProjects\volatilityModels\classes_ir.py", line 23, in <module>
ws_sheet1.Cells(1, 'A').Value = '=TR("USDSROIS=";"BID;ASK;TRDPRC_1;HST_CLOSE";"CH=Fd RH=IN";B2)'
File "C:\ProgramData\Anaconda3\envs\volatilityModels\lib\site-packages\win32com\client\__init__.py", line 595, in __setattr__
self._oleobj_.Invoke(*(args + (value,) + defArgs))
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2146827284), None)
Since Reuters Excel add-in requires a sign-in, I thought this might be the problem, so following from this post, I tried:
os.startfile(fr'{path_onedrive}\reuters\ReutersExcel.xlsx')
xlApp = client.GetObject(None, 'Excel.Application')
xlApp.Visible = True
where, ReutersExcel.xlsx is just an empty Excel file with the add-in signed in already. However, from here, I am not sure how to continue since client.GetObject and client.Dispatch don't return the same object with the same functions.
Cells(1,1).Value = ...? And also try setting theFormulaproperty, notValue.