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

我找到了openpyxl程序包,但是我不明白如何使它读取活动的打开的工作簿?

最佳答案

我不是100%确定此解决方案是否可以与DDE一起使用(文件是否正在写入磁盘的某个位置?)

我建议使用xlrdhttps://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))

关于python - python库或代码以读取已打开的Excel文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28356985/

10-09 08:33
查看更多