python库或代码读取已经打开的Excel文件 [英] python library or code to read already open Excel file

查看:1753
本文介绍了python库或代码读取已经打开的Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我已经打开了一个Excel 2013文件(例如XYZ.xlsx) - 该文件通过DDE链接获取一些数据。假设我想从工作表中读取某些单元格(如A1:B3)(例如Sheet1)。我如何在Python 3中执行(我使用的是Python 3.4.2.4 / Winpython安装)?



我发现 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆