从文件中读取第5列含有‘手机’的行,并输出满足条件的第二、五列数据。
import os
from openpyxl import load_workbookfull_fn = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'exam.xlsx')
wb = load_workbook(full_fn)
sh = wb['Sheet1']
rows = sh.max_row
for i in range(1,rows+1):i2=cell(row=i,column=2).valuei5=cell(row=i,column=5).valueif i5 is None:passelif u'手机' in i5:print(i2+'\t'+i5)
wb.close()
注: sh = wb['Sheet1'] 已替代之前的老版本函数 sh = wb.excel.get_sheet_by_name('Sheet1');尽管它目前还可以工作,但python有"已弃用函数"的警告提示。