python - 通过excel使用python从bloomberg api获取数据
问题描述
我编写了一些代码,通过使用python在excel中编写查询成功地从bloomberg api中提取数据,然后启动excel通过bloomberg插件获取数据,将数据传输到csv(为了更容易在python和其他下游需求)和回报。
唯一的问题是,为了将数据提取到 excel 中,我必须在保存文件之前设置 time.sleep(40),因为通过 excel 的连接需要足够的时间来提取数据。我想知道是否有一种方法可以自动检测何时提取数据?- 计划是将其扩展为在一个循环中或跨多个线程执行许多查询,为了提高效率,我需要检测何时提取数据以开始下一步。
有什么想法真的很有帮助吗?查看函数 run_VBA 到目前为止,这是我的代码:
import xlsxwriter
import pandas as pd
import xlwings as xl
import glob
import openpyxl
import time
import os
import win32com.client
def write_bloomberg_query_in_excel():
"""main function, make an excel workbook containing an api query, open the file and allow the data to
be pulled from the api, save and close, then transfer the data into a pandas df and csv"""
WB = 'C:/python_workspace/bloomberg_api_data_pull/excel_queries/daily_wind_temp_precip.xlsx'
location = "EGLL"
make_workbook(WB, location)
run_VWA(WB, location)
df = df_from_excel(WB, location) # sheetname is optional
df.to_csv(WB.split('.')[0]+'.csv', index=False)
return
def run_VWA(WB, location):
"""open the excel file, allow enough time to pull the data, then close and save"""
bb = 'C:/blp/API/Office Tools/BloombergUI.xla'
xl=win32com.client.DispatchEx("Excel.Application")
xl.Workbooks.Open(bb)
xl.AddIns("Bloomberg Excel Tools").Installed = True
wb = xl.Workbooks.Open(Filename=WB) #opens workbook in readonly mode.
xl.Visible = False
# need help here!! this time works for this query but I need to scale
# the api calls and need a way to select time or detect when
# the download has happened
time.sleep(40)
wb.Close(SaveChanges=1)
xl.Quit()
#Cleanup the com reference.
del xl
return
def make_workbook(WB, location):
"""write a bloomberg api query into an an excel workbook """
# Create a workbook and add a worksheet.
workbook = xlsxwriter.Workbook(WB)
worksheet = workbook.add_worksheet(location)
# Some data we want to write to the worksheet.
W = """=BSRCH("comdty:weather","provider=wsi","location={}",
"model=ACTUALS","frequency=DAILY","target_start_date=2018-08-01",
"target_end_date=2018-12-31",
"fields=WIND_SPEED|TEMPERATURE|PRECIPITATION_24HR")""".format(location)
# write to worksheet using formula
worksheet.write(0, 0, W)
# close
workbook.close()
return
def df_from_excel(path, SN):
"""read the contents of an excel file into a pandas dataframe"""
app = xl.App(visible=False)
book = app.books.open(path)
sheet = book.sheets(SN)
book.save()
df = pd.read_excel(path, sheet_name=SN)
app.kill()
return df
if __name__=="__main__":
write_bloomberg_query_in_excel()
我知道从 python api 获取这些数据会更有意义,但是不支持我在这里执行的“bsrch”类型的查询。
但是我愿意接受更好的方法吗?最好是 python,或者可能是 R
解决方案
我建议在 R 中使用Rblpapi包,因为它具有 bsrch 功能。
推荐阅读
- html - 为什么我的图标/按钮没有加载到我的 chrome 托管网站上
- php - PHP Echo Form 操作 URL 问题
- arrays - 使用 React 从 api 获取一些数据
- django - 如何从 Django 模型访问 TagField 作为 postgres 中的列?
- c++ - C++ 异常:内存位置 0x003DF4C0 处的 std::out_of_range
- c - 使用 max_align_t 存储一大块字节
- mysql - 如何从数据库中检索贷款借款人,将 retun 月份作为列名?
- ruby - 如何在 Ruby 中将 gsub 与文件一起使用?
- javascript - 将函数作为道具传递给功能组件
- react-native - 在反应本机 webview 中使用相机和地理位置