分别对xlsx和xls文件进行区分行号和列号的返回
本文所用openpyxl、xlrd为python三方库,下载安装可直接cmd执行pip install openpyxl、pip install openpyxl。
# 获取指定内容所在单元格的行列 path:对应文件路径 sheet:对应sheet名称 character:指定内容
def get_row_xlsx(path, sheet, character):
import openpyxl
wb = openpyxl.load_workbook(path)
st = wb[sheet]
for x in range(1, st.max_row + 1):
for y in range(1, st.max_column + 1):
if st.cell(x, y).value is not None:
text = st.cell(x, y).value
if character == str(text):
row = x
return row
def get_column_xlsx(path, sheet, character):
import openpyxl
wb = openpyxl.load_workbook(path)
st = wb[sheet]
for x in range(1, st.max_row + 1):
for y in range(1, st.max_column + 1):
if st.cell(x, y).value is not None:
text = st.cell(x, y).value
if character == str(text):
col = y
return col
def get_row_xls(path, sheet, character):
import xlrd
wb = xlrd.open_workbook(path)
st = wb.sheet_by_name(sheet)
for x in range(st.nrows):
for y in range(st.ncols):
if st.cell_value(x, y) is not None:
text = st.cell_value(x, y)
if character == str(text):
row = x
return row
def get_column_xls(path, sheet, character):
import xlrd
wb = xlrd.open_workbook(path)
st = wb.sheet_by_name(sheet)
for x in range(st.nrows):
for y in range(st.ncols):
if st.cell_value(x, y) is not None:
text = st.cell_value(x, y)
if character == str(text):
col = y
return col
原创文章,转载请注明出处,谢谢配合!