Skip to main content
 首页 » 编程设计

python之打开特定的 excel 文件/工作表以获取特定的内容单元格位置

2025年05月04日9虾米哥

我有一个 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 

请您参考如下方法:

你不需要制作字典。遍历 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