简体   繁体   English

如何使用excel RTD和python接收excel的实时数据?

[英]How to receive real-time data in excel using excel RTD and python?

I want to use excel as a front-end, which will continuously update multiple tables in real-time (every 2 seconds).我想使用 excel 作为前端,它会实时(每 2 秒)连续更新多个表。 I have a python program that prepares all the data.tables, but it is running on some other server.我有一个准备所有 data.tables 的 python 程序,但它正在其他服务器上运行。 I am storing python data in a Redis cache as a key-value pair.我将 python 数据作为键值对存储在 Redis 缓存中。 Eg例如

'bitcoin':'bitcoin,2021-04-23 14:23:23,49788,dollars,4068890,INR,100000'

'doge':'doge,2021-04-23 14:23:23,0.2334,dollars,21,INR,1000'  

But, now I also want to use the same data in excel. Furthermore, I found that I can use excel RTD functions to update data in excel in real-time.但是,现在我也想使用excel中的相同数据。此外,我发现我可以使用excel RTD功能实时更新excel中的数据。 But, I have no idea how will python send data to the excel RTD function. As per my understanding, I need to set up some RTD server in python and that will inject data to the excel RTD function. But how?, I am not quite sure.但是,我不知道 python 将如何向 excel RTD function 发送数据。据我了解,我需要在 python 中设置一些 RTD 服务器,这会将数据注入 excel RTD 8834008 am.65898 但不是吗?非常肯定。 Please help me with the required infrastructure or any code examples in python.请帮助我所需的基础设施或 python 中的任何代码示例。

Note: I cannot use xlwings and pyxll(paid) for some reasons.注意:由于某些原因,我无法使用 xlwings 和 pyxll(付费)。

Thanking you in advance.提前谢谢你。

I want to use excel as a front-end, which will continuously update multiple tables in real-time (every 2 seconds).我想使用 excel 作为前端,它将实时(每 2 秒)不断更新多个表。 I have a python program that prepares all the data tables, but it is running on some other server.我有一个准备所有数据表的 python 程序,但它正在其他服务器上运行。 I am storing python data in a Redis cache as a key-value pair.我将 python 数据作为键值对存储在 Redis 缓存中。 Eg例如

'bitcoin':'bitcoin,2021-04-23 14:23:23,49788,dollars,4068890,INR,100000'

'doge':'doge,2021-04-23 14:23:23,0.2334,dollars,21,INR,1000'  

But, now I also want to use the same data in excel.但是,现在我也想在 excel 中使用相同的数据。 Furthermore, I found that I can use excel RTD functions to update data in excel in real-time.此外,我发现我可以使用 excel RTD 函数实时更新 excel 中的数据。 But, I have no idea how will python send data to the excel RTD function.但是,我不知道 python 将如何将数据发送到 excel RTD function。 As per my understanding, I need to set up some RTD server in python and that will inject data to the excel RTD function.据我了解,我需要在 python 中设置一些 RTD 服务器,并将数据注入 excel RTD function。 But how?, I am not quite sure.但是如何?,我不太确定。 Please help me with the required infrastructure or any code examples in python.请帮助我提供所需的基础架构或 python 中的任何代码示例。

Note: I cannot use xlwings and pyxll(paid) for some reasons.注意:由于某些原因,我不能使用 xlwings 和 pyxll(paid)。

Thanking you in advance.提前谢谢你。

You can do this with xlOil which is free (disclaimer: I wrote it).您可以使用免费的xlOil执行此操作(免责声明:我写的)。 It allows you to write an async generator function in python which is presented to Excel as an RTD function.它允许您在 python 中编写一个异步生成器 function,它作为 RTD function 呈现给 Excel。

As an example, the following code defines an RTD worksheet function pyGetUrl which will fetch a URL every N seconds.例如,以下代码定义了一个 RTD 工作表 function pyGetUrl ,它将每 N 秒获取一个 URL。 I'm not familiar with Redis, but I can see several async python client libraries which should be able to replace aiohttp in the below to access your data.我不熟悉 Redis,但我可以看到几个异步 python 客户端库,它们应该能够替换下面的aiohttp来访问您的数据。

import aiohttp
import ssl
import xloil as xlo

# This is the implementation: it pulls the URL and returns the response as text
async def _getUrlImpl(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, ssl=ssl.SSLContext()) as response:
           return await response.text() 

#
# We declare an async gen function which calls the implementation either once,
# or at regular intervals
#
@xlo.func(local=False, rtd=True)
async def pyGetUrl(url, seconds=0):
    yield await _getUrlImpl(url)
    while seconds > 0:
        await asyncio.sleep(seconds)
        yield await _getUrlImpl(url)

Rolling your own RTD server using Excel's COM interface and pywin32 may also be viable, you can look at this python example to see it done.使用 Excel 的 COM 接口和pywin32滚动您自己的 RTD 服务器也可能是可行的,您可以查看这个python 示例以了解它的完成情况。 You'll need to add a ProgId and CLSID to the windows registry so Excel can find your server;您需要将 ProgId 和 CLSID 添加到 windows 注册表,以便 Excel 可以找到您的服务器; the example show you how to do this.该示例向您展示了如何执行此操作。 Fair warning : this recent questioner was unable to make the example work.公平警告这位最近的提问者无法让这个例子发挥作用。 I also tried the example and had even less luck, so some debugging may be required.我也试过这个例子,但运气更差,所以可能需要进行一些调试。

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

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