windows/linux 安装openpyxl
pip install openpyxl
查找某Excel中是否存在某字符串text
# 输入:
# file_path: Excel文件名
# search_text: 要查找的字符串
# 输出:
# 查到: [sheetname, cell.row, cell.column]组成的数组
# 未查到: None
def find_text_in_excel(file_path, search_text):
workbook = openpyxl.load_workbook(file_path)
for sheetname in workbook.sheetnames:
#无法从workbook直接得到sheet,需要先获取sheetname作为索引
sheet = workbook[sheetname]
for row in sheet.rows:
for cell in row:
if cell.value == search_text:
print(f"Found '{search_text}' in '{sheetname}': {cell.coordinate}")
return [sheetname, cell.row, cell.column]
print(f"'{search_text}': Not found in {os.path.basename(file_path)}")
return None
标签:search,python,text,Excel,cell,sheetname,path,workbook,操作 From: https://www.cnblogs.com/moon-sun-blog/p/18129199