[Python] 纯文本查看 复制代码 # -*- coding:utf-8 -*-
import openpyxl
from openpyxl import load_workbook
wb = load_workbook(filename='./1.xlsx') # 表格所在位置,这里示例是在项目根目录下,是个名为“1.xlsx”的表格。仅支持.xlsx格式。
ws = wb.worksheets[0]
max_row = ws.max_row
max_col = ws.max_column
def value_by_col(value, col):
"""
:param value: 查询的值,需要与对应单元格数据类型一致。如查整数用int,文本用str,小数用flote
:param col: 要查询的列号,从1开始,A对应1,B对应2,依次类推
:return: NULL
"""
# 针对指定列查询
re = False
for row in range(max_row):
use_cell = ws.cell(row + 1, col)
if value == use_cell.value:
print("已找到,在%s单元格"% use_cell.coordinate)
re = True
if re:
pass
else:
print("未找到符合的单元格")
def value_by_row(value, row):
"""
针对指定行查询
:param value: 要查询的值,需要与对应单元格数据类型一致。如查整数用int,文本用str,小数用flote
:param row: 要查询的行号,从1开始
:return: NULL
"""
re = False
for col in range(max_col):
use_cell = ws.cell(row, col + 1)
# print(use_cell.value, type(use_cell.value))
if value == use_cell.value:
print("已找到,在%s单元格" % use_cell.coordinate)
re = True
if re:
pass
else:
print("未找到符合的单元格")
def value_by_all(value):
"""
针对execl全表查询
:param value: 要查询的值,需要与对应单元格数据类型一致。如查整数用int,文本用str,小数用flote
:return: NULL
"""
#
re = False
for col in range(max_col):
for row in range(max_row):
use_cell = ws.cell(row + 1, col + 1)
if use_cell.value == value:
re = True
print("已找到,在%s单元格" % use_cell.coordinate)
if re:
pass
else:
print("未找到符合的单元格")
if __name__ == "__main__":
# 根据需要调用对应的函数
value_by_col('20190871540000317312', 6) # 在第6列(即F列)查找
value_by_row('20190871540000317312', 4) # 在第4行查找
value_by_all('20190871540000317312') # 全表查找
|