简体   繁体   English

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

[英]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.我有一堆 excle 工作表,它们提取了我在 Python 中用于各种事情的Bloomberg 日终数据。 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.传统的刷新都不起作用,就像在excel中按f9不会刷新它们一样。 The only way I know how to do it is clicking (in excel) under bloomberg tab the refresh icon, the two arrows.我知道如何做到这一点的唯一方法是单击(在 excel 中)bloomberg 选项卡下的刷新图标,两个箭头。 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.这适用于重新计算一般的 excel 计算公式,但不会刷新Bloomberg(=BHD 类型)公式。

Any suggestions welcomed!欢迎任何建议!

There are two options: Simple solution create macro in sheet (will need to make file xlsm rather than xlsx)有两个选项: 简单的解决方案在工作表中创建宏(需要制作文件 xlsm 而不是 xlsx)

sub refresh_bbg()子 refresh_bbg()

'depending on your setup may be worth checking Bloomberg api is installed here and installing if not. '取决于您的设置,可能值得检查 Bloomberg api 是否安装在此处,如果没有则安装。

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

end sub结束子

Then in python然后在python中

global_sprdsheet.Application.Run("global.xlsm!refresh_bbg") 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.但我认为更好的解决方案是通过 tia 或 pdblp 在 python 中进行 bbg 数据收集。 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.我已经使用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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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