python 要读取已打开的 Excel 文件的库或代码
python library or code to read already open Excel file
假设我已经打开了一个 Excel 2013 文件(比如 XYZ.xlsx)- 该文件通过 DDE link 获取一些数据。假设我想从工作表(比如 Sheet1)中读取某些单元格(比如 A1:B3)。 Python 3(我使用的是Python 3.4.2.4 / Winpython安装)怎么办?
我找到了 openpyxl
包,但我不明白如何让它读取活动打开的工作簿?
我不是 100% 确定此解决方案是否适用于 DDE(文件是否正在写入磁盘的某处?)
我建议使用 xlrd
(https://github.com/python-excel/xlrd)。我亲自通过 pip 安装了它。然后简单地阅读 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))
我需要完全相同的解决方案,这就是我所做的(它已经有效):
我使用 Node.js 在不同端口(8080 和 3000)上创建了一个本地 http 服务器和一个本地 websocket 服务器(使用 Node Webkit,但您可以使用纯 Node.js ).
在 http 服务器上,我设置 http://localhost:8080/write/ 接收带有我想要的参数的 GET 请求,并使用这些参数向 websocket 服务器广播消息。
我写了一个VBA代码来将数据推送到本地http服务器,使用Worksheet_Calculate sub(这是每次工作sheet刷新时触发的事件其内容)。
最后做了Python监听websocket服务器(ws://localhost:3000/),等待服务器信号
深入编码细节:
为了创建本地服务器,我使用了 Express 和 ws 模块。第一个创建http服务器,第二个创建websocket服务器:
快递详情:
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
这里的技巧细节是如何知道何时已经发送了一些数据,这样您就可以避免发送两次或更多次。您必须根据数据在 sheet 上的组织方式来创建您的方法。对我来说,有一个数字列,有序的和一个注册我发送的最后一个数字的单元格。
好的!现在您必须准备 Python 以接收此数据:
我下载了 websocket-client 0.25.0 模块。这是监听 websocket 服务器的 python 代码:
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()
如果您在本地网络上,您可以使用 IP(或机器名称)post 并监听服务器(这太棒了)。这是使用 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;
}
});
}
希望对您有所帮助。如果你提出一些问题,请告诉我。
我想分享一个主要从这个 窃取的解决方案,因为我发现 xlrd 在打开 Excel 文件时会产生不一致的结果,这些文件可能会或可能不会被其他用户打开网络。使用 win32com(到目前为止)对我来说效果更好。
#using path to avoid escape character errors
from pathlib import Path
path = Path(r'/full/filepath')
from win32com import client
xlApp = client.GetObject(None, "Excel.Application")
books = xlApp.Workbooks.Open(path)
ws = books.Worksheets[0]
ws.Visible = 1
#returns tuple
shift_1 = ws.Range("A1:B2").Value
import numpy as np
#selection of 0 element of tuple would depend on use case.
shift_1 = np.asarray(shift_1[0])
假设我已经打开了一个 Excel 2013 文件(比如 XYZ.xlsx)- 该文件通过 DDE link 获取一些数据。假设我想从工作表(比如 Sheet1)中读取某些单元格(比如 A1:B3)。 Python 3(我使用的是Python 3.4.2.4 / Winpython安装)怎么办?
我找到了 openpyxl
包,但我不明白如何让它读取活动打开的工作簿?
我不是 100% 确定此解决方案是否适用于 DDE(文件是否正在写入磁盘的某处?)
我建议使用 xlrd
(https://github.com/python-excel/xlrd)。我亲自通过 pip 安装了它。然后简单地阅读 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))
我需要完全相同的解决方案,这就是我所做的(它已经有效):
我使用 Node.js 在不同端口(8080 和 3000)上创建了一个本地 http 服务器和一个本地 websocket 服务器(使用 Node Webkit,但您可以使用纯 Node.js ).
在 http 服务器上,我设置 http://localhost:8080/write/ 接收带有我想要的参数的 GET 请求,并使用这些参数向 websocket 服务器广播消息。
我写了一个VBA代码来将数据推送到本地http服务器,使用Worksheet_Calculate sub(这是每次工作sheet刷新时触发的事件其内容)。
最后做了Python监听websocket服务器(ws://localhost:3000/),等待服务器信号
深入编码细节:
为了创建本地服务器,我使用了 Express 和 ws 模块。第一个创建http服务器,第二个创建websocket服务器:
快递详情:
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
这里的技巧细节是如何知道何时已经发送了一些数据,这样您就可以避免发送两次或更多次。您必须根据数据在 sheet 上的组织方式来创建您的方法。对我来说,有一个数字列,有序的和一个注册我发送的最后一个数字的单元格。
好的!现在您必须准备 Python 以接收此数据:
我下载了 websocket-client 0.25.0 模块。这是监听 websocket 服务器的 python 代码:
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()
如果您在本地网络上,您可以使用 IP(或机器名称)post 并监听服务器(这太棒了)。这是使用 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;
}
});
}
希望对您有所帮助。如果你提出一些问题,请告诉我。
我想分享一个主要从这个
#using path to avoid escape character errors
from pathlib import Path
path = Path(r'/full/filepath')
from win32com import client
xlApp = client.GetObject(None, "Excel.Application")
books = xlApp.Workbooks.Open(path)
ws = books.Worksheets[0]
ws.Visible = 1
#returns tuple
shift_1 = ws.Range("A1:B2").Value
import numpy as np
#selection of 0 element of tuple would depend on use case.
shift_1 = np.asarray(shift_1[0])