python库或代码读取已经打开的Excel文件 [英] python library or code to read already open Excel file
问题描述
我发现 openpyxl
包,但是我不明白如何让它读取一个活跃的打开的工作簿?
我不是100 %确定这个解决方案是否适用于DDE(该文件是否被写入某个地方的磁盘?)
我建议使用 xlrd
( https://github.com/python-excel/xlrd )。我个人通过pip安装它。然后简单地阅读A1:B3做(从文档中删除):
从xlrd import open_workbook
wb = open_workbook('spreadsheet.xlsx')
在wb.sheets()中的s:
print('Sheet:',s.name)#你可以在这里检查工作表名称如果要在范围(2)中的行
:#前2行(A和B)(使用s.nrows为所有)
values = []
for col in range( 3):#前3列(全部使用s.ncols)
values.append(str(s.cell(row,col).value))
print(','。join值))
Suppose I have an Excel 2013 file already open (say XYZ.xlsx) - this file gets some data via a DDE link. Suppose I want to read certain cells (say A1:B3) from a worksheet (say Sheet1). How can I do it in Python 3 (I am using Python 3.4.2.4 / Winpython installation)?
I found the openpyxl
package, but I couldn't understand how to make it read an active open workbook?
I am not 100% sure if this solution works with DDE (is the file being written to disk somewhere?)
I recommend using xlrd
(https://github.com/python-excel/xlrd). I personally installed it via pip. Then to simply read A1:B3 do as such (ripped off the docs):
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))
这篇关于python库或代码读取已经打开的Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!