Python using win32com wont update excel sheet with needed Add-ins

六月ゝ 毕业季﹏ 提交于 2021-02-10 14:16:41

问题


I am trying to get a python script to create an excel file with a formula ready to be executed when the workbook opens(it requires an add-in only available in excel). Once open, excel will compute the formula and python will close and save the file. I can then use this excel file for the rest of my python script. However I can't get the add in to execute the formula until I manually open the python created excel file.

I have tried various ways to get win32com to open the excel file with all add-ins loaded. However, none seem to work.

workbook = xlsxwriter.Workbook('C:/Users/ccraig/Desktop/QR Price_Bonds/t.xlsx')
worksheet1=workbook.add_worksheet()
worksheet2=workbook.add_worksheet()
worksheet1.write(1,1,'Hello')
worksheet1.write_formula('A2','=BDH(Sheet2!A1&"@TRAC Corp","TRADE",Sheet2!B2,Sheet2!BA2,"IntrRw=True","RPSCodes=S","RPTContra=S","Price=S","Type=S","Yield=S","Size=S","IndicatorCodes=S","CondCodes=S","QRM=S","Dir=V","cols=9;rows=312")')
worksheet2.write('A1','1730T0R50')
worksheet2.write('B2','8/7/2019')
worksheet2.write('A2','8/7/2019')
workbook.close()
xlapp = win32com.client.DispatchEx("Excel.Application")
xlapp.RegisterXLL('C:/blp/API/Office Tools/bofaddin.dll')
# for addin in xlapp.AddIns:
#     addin.Installed=True
wb = xlapp.Workbooks.Open('C:/Users/ccraig/Desktop/QR Price_Bonds/t.xlsx',None,False)
wb_addin = ('C:/blp/API/Office Tools/bofaddin.dll')
wb.RefreshAll()
xlapp.CalculateUntilAsyncQueriesDone()
wb.Save()
xlapp.Quit()

I don't get any errors. The file is created as desired, but the formula wont execute until i manually open the file.


回答1:


xlapp = win32com.client.DispatchEx("Excel.Application")
xlapp.RegisterXLL('C:/blp/API/Office Tools/bofaddin.dll')
xlapp.Workbooks.Open('C:\\blp\\API\\Office Tools\\BloombergUI.xla')
wb = xlapp.Workbooks.Open(filepath,None,False)
xlapp.Visible = True
wb_addin = ('C:/blp/API/Office Tools/bofaddin.dll')
wb.RefreshAll()
sleep(60)
wb.Save()
xlapp.Quit()

Here is the working code.




回答2:


I believe the Bloomberg API in Excel needs both the .dll and the xla. Try adding:

xlapp.Workbooks.Open('C:\\blp\\API\\Office Tools\\BloombergUI.xla')

I didn't want to test your code for fear of hitting my monthly data limit, but your RegisterXLL was the key to solving my issue.



来源:https://stackoverflow.com/questions/57714123/python-using-win32com-wont-update-excel-sheet-with-needed-add-ins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!