【问题标题】:How to receive real-time data in excel using excel RTD and python?如何使用excel RTD和python在excel中接收实时数据?
【发布时间】:2021-04-24 07:25:55
【问题描述】:

我想用 excel 作为前端,它会实时(每 2 秒)不断更新多个表。我有一个准备所有数据表的 python 程序,但它在其他一些服务器上运行。我将 python 数据作为键值对存储在 Redis 缓存中。 例如

'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'  

但是,现在我也想在 excel 中使用相同的数据。此外,我发现我可以使用excel RTD函数实时更新excel中的数据。但是,我不知道 python 将如何将数据发送到 excel RTD 函数。 据我了解,我需要在 python 中设置一些 RTD 服务器,并将数据注入到 excel RTD 函数中。但是如何?,我不太确定。请帮助我提供所需的基础设施或 python 中的任何代码示例。

注意:由于某些原因,我不能使用 xlwings 和 pyxll(paid)。

提前谢谢你。

【问题讨论】:

    标签: python excel rtd


    【解决方案1】:

    您可以使用免费的xlOil 来执行此操作(免责声明:我写的)。它允许您在 python 中编写一个异步生成器函数,该函数作为 RTD 函数呈现给 Excel。

    例如,以下代码定义了一个 RTD 工作表函数pyGetUrl,它将每 N 秒获取一个 URL。我不熟悉 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)
    

    使用 Excel 的 COM 接口和 pywin32 滚动您自己的 RTD 服务器也可能是可行的,您可以查看 this python 示例以了解它是否已完成。您需要将 ProgId 和 CLSID 添加到 Windows 注册表,以便 Excel 可以找到您的服务器;该示例向您展示了如何执行此操作。 公平警告this 最近的提问者无法使示例工作。我也试过这个例子,运气更差,所以可能需要一些调试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-05
      • 2015-05-13
      • 1970-01-01
      • 2012-04-16
      • 1970-01-01
      • 2017-02-06
      • 1970-01-01
      • 2015-10-04
      相关资源
      最近更新 更多