[英]Open specific excel file/sheet to get a specific content cell location
I have a list of excel files and their corresponding sheet number. 我有一个excel文件及其对应的工作表编号的列表。 I need python to go to those sheets and find out the cell location for a particular content.
我需要python转到这些工作表,并找到特定内容的单元格位置。 Can someone point out the error in my code?
有人可以指出我的代码中的错误吗? Thanks in advance
提前致谢
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
You don't need to make a dictionary. 您无需制作字典。 Iterate over
zip(filename, sheetnumber)
: 遍历
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.