--coding:utf8 --
import xlrd
import os
def check():
for root,dirs,files in os.walk(work_url):
for file in files:
path=os.path.join(root,file)
if path.endswith('.xls')or path.endswith('.xlsx'):
workbook = xlrd.open_workbook(path)
sheet_names = workbook.sheet_names() # 返回表名list
for sheet in range(len(sheet_names)):
work_sheet = workbook.sheet_by_name(sheet_names[sheet])
nrow = work_sheet.nrows
ncol = work_sheet.ncols
for i in range(nrow):
for j in range(ncol):
val=str(work_sheet.cell_value(i,j))
if keyword in val: #判断是否存在关键字,用in不能用==
row=i+1
col=j+1
#列数转为excel列数
if col <= 26: # 兼容26以内
col = chr(col + 64)
else: # 兼容26以上到676
if col % 26 == 0:
m = chr(col // 26 - 1 + 64)
n = chr(64 + 26)
else:
m = chr(col // 26 + 64) # python3只取整数需要使用// python2取整数要转为浮点型
n = chr(col% 26 + 64)
col = m + n
print(path,'\t',sheet_names[sheet],'\t',str(col)+str(row))
if val_show==True: #是否显示匹配值
print(val)
if __name__ == '__main__':
work_url = r'C:\Users\swt1051\Desktop\检测目录'
keyword = 'ubGold'
val_show=True #是否显示匹配值
print('keyword为:\t'+keyword)
print('==================检索中====================')
try:
check()
except Exception:
print('数据存在异常!!!!!!!!!!!')
print('关闭当前的检索文档,或者删除excel的tmp文件')