python库或代码来读取已经打开的Excel文件

假设我已经打开了一个Excel 2013文件(比如XYZ.xlsx) – 这个文件通过一个DDE链接获取了一些数据。 假设我想从工作表中读取某些单元格(比如A1:B3)(比如Sheet1)。 我怎样才能在Python 3(我正在使用Python 3.4.2.4 / Winpython安装)?

我find了openpyxl包,但是我不明白如何让它读取一个活动的打开工作簿?

我不是100%确定,如果这个解决scheme与DDE一起工作(文件被写入磁盘的某个地方?)

我推荐使用xlrdhttps://github.com/python-excel/xlrd )。 我亲自安装它通过点。 然后简单地阅读A1:B3就这样(撕下文档):

 from xlrd import open_workbook wb = open_workbook('spreadsheet.xlsx') for s in wb.sheets(): print('Sheet:',s.name) # you can check for the sheet name here if you want for row in range(2): #the first 2 rows (A and B) (use s.nrows for all) values = [] for col in range(3): #the first 3 columns (use s.ncols for all) values.append(str(s.cell(row,col).value)) print(','.join(values)) 

我需要完全相同的解决scheme,这是我所做的(它已经工作):

  1. 我用Node.js(使用Node Webkit,但可以使用纯Node.js)在不同的端口(8080和3000)上创build本地http服务器和本地websocket服务器。

  2. 在http服务器上,我设置http:// localhost:8080 / write /来接收带有我想要的参数的GET请求,并用这些参数向websocket服务器广播消息。

  3. 我写了一个VBA代码,使用Worksheet_Calculate sub(每次工作表刷新其内容时触发的事件)将数据推送到本地http服务器。

  4. Finnaly我让Python听websocket服务器(ws:// localhost:3000 /),等待服务器信号。


潜入编码细节:

为了创build本地服务器,我使用了Express和ws模块。 第一个创buildhttp服务器,第二个创buildwebsocket服务器:

快递详情:

 var express = require('express'); var app = express(); app.get('/', function (req, res) { res.send('Hello World!') }) // to see if server is on // broadcast data on q GET variable: http://localhost:8080/write/?q=32,34,23 app.get('/write/', function (req, res) { wss.broadcast(req.query.q); // this line uses the variable created below res.send(); }); var server = app.listen(8080, function () {}); 

ws细节:

 var WebSocketServer = require('ws').Server; wss = new WebSocketServer({port: 3000}); wss.on('connection', function(ws) { // you can use this event to perform some action when python get connected ws.send("you're connected"); // use this event if you need websocket to receive message ws.on('message', function(message) { ws.send(message); }); }); wss.broadcast = function broadcast(data) { wss.clients.forEach(function each(client) { client.send(data); }); }; 

将数据从Excel发送到http服务器(我尝试使用websocket,但是我无法弄清Excel如何连接到websocket):

 Function postServer(url, data) Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP") objHTTP.Open "GET", url & data, False ' true para asynchronous calls objHTTP.send '(dados) postServer = 1 'It's a good idea to get the responseHTML from server and verifies if the everything is all right End Function 

在Worksheet_Calculate()中使用这个函数来发送数据:

 Private Sub Worksheet_Calculate() server = "http://localhost:8080/write/?q=" 'data = <something> 'catch your data with vba code postServer(server,data) End sub 

这里的技巧细节是如何知道什么时候一些数据已经发送,所以你可以避免发送两次或更多。 您必须根据数据在工作表上的组织方式来创build方法。 对我来说,有一个号码栏,命令和一个单元登记我发送的最后一个号码。

好! 现在你必须准备Python来接收这些数据:

我下载了websocket-client 0.25.0模块。 这里是python代码来监听websocket服务器:

 import sys import websocket import thread import time wsAddress = "ws://localhost:3000/" def on_message(ws, message): print message # you got it! def on_error(ws, error): print error def on_close(ws): print "### closed ###" def on_open(ws): print "conectado" if __name__ == "__main__": websocket.enableTrace(True) ws = websocket.WebSocketApp(wsAddress, on_message = on_message, on_error = on_error, on_close = on_close) ws.on_open = on_open ws.run_forever() 

如果您在本地networking上,则可以使用IP(或机器名称)发布并聆听服务器(这太棒了)。 以下是使用node.js发现id的代码:

 var os = require('os'); var ifaces=os.networkInterfaces(); for (var dev in ifaces) { var alias=0; ifaces[dev].forEach(function(details){ if ((details.family=='IPv4')&&(details.address!="127.0.0.1")&&(details.internal === false)) { console.log(details.address); ++alias; } }); } 

我希望我能帮上忙 如果您提出一些问题,请告诉我。