简体   繁体   English

xlrd-将数据附加到已打开的工作簿

[英]xlrd - append data to already opened workbook

I am trying to write a python program for appending live stock quotes from a csv file to an excel file (which is already open) using xlrd and xlwt. 我正在尝试编写一个Python程序,使用xlrd和xlwt将CSV文件中的实时股票报价附加到excel文件(已打开)中。

The task is summarised below. 任务总结如下。

From my stock-broker's application, a csv file is continually being updated on my hard disk. 从我的股票经纪人的应用程序中,一个csv文件正在不断地在我的硬盘上更新。 I wish to write a program which, when run, would append the new data from csv file to an excel file, which is kept open (I wonder whether it is possible to read & write an open file). 我希望编写一个程序,该程序在运行时会将csv文件中的新数据附加到excel文件中,该文件保持打开状态(我想知道是否可以读取和写入打开的文件)。

I wish to keep the file open because I will be having stock-charts in it. 我希望保持打开状态,因为我将在其中保存库存图表。

Is it possible? 可能吗? If yes, how? 如果是,怎么办?

Not directly. 不直接。 xlutils can use xlrd and xlwt to copy a spreadsheet, and appending to a "to be written" worksheet is straightforward. xlutils可以使用xlrd和xlwt复制电子表格,并且将其附加到“待写”工作表很简单。 I don't think reading the open spreadsheet is a problem -- but xlwt will not write to the open book/sheet. 我认为阅读打开的电子表格不是问题,但是xlwt不会写到打开的书/表中。

You might write an Excel VBA macro to draw the graphs. 您可以编写一个Excel VBA宏来绘制图形。 In principle, I think a macro from a command workbook could close your stock workbook, invoke your python code to copy and update, open the new spreadsheet, and maybe run the macro to re-draw the graphs. 原则上,我认为命令工作簿中的宏可以关闭您的库存工作簿,调用您的python代码进行复制和更新,打开新的电子表格,还可以运行该宏以重新绘制图形。

Another approach is to use matplotlib for the graphs. 另一种方法是对图表使用matplotlib。 I'd think a sleep loop could wake up every n seconds, grab the new csv data, append it to your "big" csv data, and re-draw the graph. 我认为睡眠循环可以每n秒唤醒一次,获取新的csv数据,将其附加到“大” csv数据中,然后重新绘制图形。 Taking this approach keeps you in python and should make things a lot easier, imho. 采用这种方法可以使您一直处于python状态,应该使事情变得容易得多,恕我直言。 Disclosure: my Python is better than my VBA. 披露:我的Python比我的VBA好。

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

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