用于刷新电子表格的 BLOOMBERG 数据的 python 脚本

问题描述 投票:0回答:3

我有一堆 Excel 表,可以提取 Bloomberg 日终数据,我将其用于 Python 中的各种操作。我的问题如下:

有没有办法告诉sheet刷新bloomberg数据?

传统的刷新都不起作用,就像在Excel中按f9不会刷新它们一样。我知道如何做到这一点的唯一方法是单击(在 Excel 中)Bloomberg 选项卡下的刷新图标,两个箭头。我很想自动化。

这就是我现在拥有的:

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()

它适用于重新计算一般 Excel 计算公式,但不会刷新 Bloomberg(=BHD 类型)公式。

欢迎任何建议!

python excel python-3.x bloomberg
3个回答
2
投票

有两种选择: 简单的解决方案 在工作表中创建宏(需要创建文件 xlsm 而不是 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

然后在Python中

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

但我认为更好的解决方案是通过 tia 或 pdblp 在 python 中进行 bbg 数据收集。然后将数据写入文件。根据我的经验,这更可靠。


1
投票

我使用win32com打开excel并将其置于顶部并开始一一按下按钮,延迟1秒,然后保存并关闭它。

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()

0
投票

@brian Vieira 的回答非常好,尽管有点“hacky”。我们确实必须使用

time
模块来给 Bloomberg 时间刷新,因为我找不到更好的方法。下面是我希望更强大的解决方案。关键点还是
time
延迟和
Application.Run("RefreshEntireWorksheet")
,这纯粹是BBG插件命令

from pathlib import Path
import win32com.client
import time

def refresh(xl, f_path):
    xl.Visible = True
    # load wb and refresh
    print ("loading wb, refreshing & saving... ")
    wb = xl.Workbooks.Open(f_path, None, ReadOnly = False)
    xl.Application.Run("RefreshEntireWorksheet")

    ws = wb.ActiveSheet
    ws.EnableCalculation = False
    time.sleep(10)
    ws.EnableCalculation = True
    ws.Calculate()

    wb.Close(SaveChanges=1)
    print ("done!")


source_dir = Path (r'C:\Users\Ako\Desktop\ako_files\work_sheets')
p_file = Path(source_dir /'global.xlsx' )
bbg_xla = Path(r'C:\Program Files (x86)\blp\API\Office Tools/BloombergUI.xla')

if Path.exists(p_file):
    try: 
        xl = win32com.client.GetActiveObject('Excel.Application')
        print ("Running Excel instance found")
        refresh(xl, p_file)
    except:
        if Path.exists(bbg_xla): #check for add-in
            xl = win32com.client.Dispatch("Excel.Application") 
            print("Excel was not running. Starting... ")
            # load add-in     
            print ("Laoding BBG add-in...")              
            xl.Workbooks.Open(bbg_xla)
            refresh(xl, p_file)
            print("Quiting Excel...")
            xl.Application.Quit() 
            del xl        
        else: 
            print("check BBG add-on path as it may not load")
© www.soinside.com 2019 - 2024. All rights reserved.