简体   繁体   English

使用Python实时更新Excel工作表

[英]Update an Excel sheet in real time using Python

Is there a way to update a spreadsheet in real time while it is open in Excel? 有没有办法在Excel中打开时实时更新电子表格? I have a workbook called Example.xlsx which is open in Excel and I have the following python code which tries to update cell B1 with the string 'ID': 我有一个名为Example.xlsx的工作簿,它在Excel中打开,我有以下python代码,它尝试使用字符串'ID'更新单元格B1:

import openpyxl

wb = openpyxl.load_workbook('Example.xlsx')
sheet = wb['Sheet']
sheet['B1'] = 'ID'

wb.save('Example.xlsx')

On running the script I get this error: 在运行脚本时,我收到此错误:

PermissionError: [Errno 13] Permission denied: 'Example.xlsx'

I know its because the file is currently open in Excel, but was wondering if there is another way or module I can use to update a sheet while its open. 我知道它,因为该文件当前在Excel中打开,但是想知道是否有其他方法或模块可以用来在打开时更新工作表。

I have actually figured this out and its quite simple using xlwings. 我实际上已经想到了这一点,使用xlwings非常简单。 The following code opens an existing Excel file called Example.xlsx and updates it in real time, in this case puts in the value 45 in cell B2 instantly soon as you run the script. 下面的代码打开一个名为Example.xlsx的现有Excel文件并实时更新,在这种情况下,在运行脚本时立即将单元格中的值45放入。

import xlwings as xw

wb = xw.Book('Example.xlsx')
sht1 = wb.sheets['Sheet']
sht1.range('B2').value = 45

You've already worked out why you can't use openpyxl to write to the .xlsx file: it's locked while Excel has it open. 您已经解决了为什么不能使用openpyxl写入.xlsx文件的原因:它在Excel打开时被锁定。 You can't write to it directly, but you can use win32com to communicate with the copy of Excel that is running via its COM interface. 您不能直接写入它,但您可以使用win32com与通过其COM接口运行的Excel副本进行通信。

You can download win32com from https://github.com/mhammond/pywin32 . 您可以从https://github.com/mhammond/pywin32下载win32com

Use it like this: 像这样使用它:

from win32com.client import Dispatch
xlApp = Dispatch("Excel.Application")
wb=xlApp.Workbooks.Item("MyExcelFile.xlsx")
ws=wb.Sheets("MyWorksheetName")

At this point, ws is a reference to a worksheet object that you can change. 此时, ws是对可以更改的工作表对象的引用。 The objects you get back aren't Python objects but a thin Python wrapper around VBA objects that obey their own conventions, not Python's. 你得到的对象不是Python对象,而是围绕VBA对象的瘦Python包装器,它遵循自己的约定,而不是Python的。

There is some useful if rather old Python-oriented documentation here: http://timgolden.me.uk/pywin32-docs/contents.html 这里有一些有用的,如果是旧的面向Python的文档: http//timgolden.me.uk/pywin32-docs/contents.html

There is full documentation for the object model here: https://msdn.microsoft.com/en-us/library/wss56bz7.aspx but bear in mind that it is addressed to VBA programmers. 这里有对象模型的完整文档: https//msdn.microsoft.com/en-us/library/wss56bz7.aspx但请记住它是针对VBA程序员的。

您无法更改另一个应用程序正在使用的Excel文件,因为该文件格式不支持并发访问。

If you want to stream real time data into Excel from Python, you can use an RTD function. 如果要将实时数据从Python流式传输到Excel,可以使用RTD功能。 If you've ever used the Bloomberg add-in use for accessing real time market data in Excel then you'll be familiar with RTD functions. 如果您曾使用Bloomberg加载项用于访问Excel中的实时市场数据,那么您将熟悉RTD功能。

The easiest way to write an RTD function for Excel in Python is to use PyXLL. 在Python中为Excel编写RTD函数的最简单方法是使用PyXLL。 You can read how to do it in the docs here: https://www.pyxll.com/docs/userguide/rtd.html 您可以在此处的文档中了解如何操作: https//www.pyxll.com/docs/userguide/rtd.html

There's also a blog post showing how to stream live tweets into Excel using Python here: https://www.pyxll.com/blog/a-real-time-twitter-feed-in-excel/ 还有一篇博文显示如何使用Python将实时推文流式传输到Excel: https//www.pyxll.com/blog/a-real-time-twitter-feed-in-excel/

If you wanted to write an RTD server to run outside of Excel you have to register it as a COM server. 如果要编写RTD服务器以在Excel外部运行,则必须将其注册为COM服务器。 The pywin32 package includes an example that shows how to do that, however it only works for Excel prior to 2007. For 2007 and later versions you will need this code https://github.com/pyxll/exceltypes to make that example work (see the modified example from pywin32 in exceltypes/demos in that repo). pywin32软件包包含一个示例,说明如何执行此操作,但它仅适用于2007之前的Excel。对于2007及更高版本,您将需要此代码https://github.com/pyxll/exceltypes来使该示例工作(请参阅该repo中exceltypes / demos中pywin32的修改示例。

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

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