打开特定的Excel文件/表以获取特定的内容单元格位置

时间:2014-05-30 16:38:37

标签: python excel python-2.7 xlrd

我有一个excel文件列表及其相应的表格编号。我需要python去那些工作表并找出特定内容的单元格位置。有人可以在我的代码中指出错误吗?提前致谢

import xlrd
value = 'Avg.'
filename = ('C:/002 Av SW W of 065 St SW 2011-Jul-05.xls', 'C:/003 Avenue SW West of 058 Street SW 2012-Jun-23.xls')
sheetnumber = ('505840', '505608')
dictionary = dict(zip(filename, sheetnumber))
for item in dictionary:
    book = xlrd.open_workbook(item)
    sheet = book.sheet_by_name(dictionary[key])
    for row in range(sheet.nrows):
        for column in range(sheet.ncols):
            if sheet.cell(row,column).value == value:
                print row, column

1 个答案:

答案 0 :(得分:1)

您不需要制作字典。迭代zip(filename, sheetnumber)

for name, sheet_name in zip(filename, sheetnumber):
    book = xlrd.open_workbook(name)
    sheet = book.sheet_by_name(sheet_name)
    for row in range(sheet.nrows):
        for column in range(sheet.ncols):
            if sheet.cell(row,column).value == value:
                print row, column