简体   繁体   中英

python script to refresh spreadsheet's BLOOMBERG data

I have a bunch of excle sheets that pull in bloomberg end of day data which I use for various things in Python. My question is the following:

Is there a way to tell to sheet to refresh the bloomberg data?

The traditional refresh all doesn't work, just like hitting f9 inside excel won't refresh them. The only way I know how to do it is clicking (in excel) under bloomberg tab the refresh icon, the two arrows. I'd love to automate that.

This is what I have now:

import win32com.client
import time

def refr_sheet():
    app_global = win32com.client.Dispatch("Excel.Application")
    global_sprdsheet = app_global.Workbooks.open('C:\\Users\\Ako\\Desktop\\ako_files\\work_sheets\\global.xlsx')
    global_sprdsheet.RefreshAll()
    time.sleep(12)
    global_sprdsheet.Save()
    global_sprdsheet.Close()
    app_global.Quit()

Which works for recalculating the general excel calc formulas but won't refresh the bloomberg (=BHD type) formulas.

Any suggestions welcomed!

There are two options: Simple solution create macro in sheet (will need to make file xlsm rather than xlsx)

sub refresh_bbg()

'depending on your setup may be worth checking Bloomberg api is installed here and installing if not.

Application.Run ("bloombergui.xla!RefreshEntireWorkbook")

end sub

Then in python

global_sprdsheet.Application.Run("global.xlsm!refresh_bbg")

But a better solution in my opinion is to do the bbg data collection in python, via tia or pdblp. And then write the data to file. In my experience this is more reliable.

I have used win32com to open excel and bring it to the top and start pressing the buttons one by one with a 1-second delay, then save and closes it.

import time
import pyautogui
import win32com.client
from win32gui import SetForegroundWindow
xl = win32com.client.DispatchEx("Excel.Application")
wb = xl.workbooks.open("C:/file.xlsx")
xl.Visible = True
SetForegroundWindow(xl.hwnd)
time.sleep(3)
pyautogui.typewrite(['alt', 's', 'r', 'a'], interval=1)
time.sleep(3)
wb.Close(SaveChanges=1)
xl.Quit()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM