根据列名读取相应的值,代码如下:
1 #!/usr/bin/python 2 # coding=utf-8 3 __author__ = 'Paul' 4 import xlrd 5 import chardet 6 import traceback 7 def getColumnIndex(table, columnName): 8 columnIndex = None 9 #print table10 for i in range(table.ncols):11 #print columnName12 #print table.cell_value(0, i)13 if(table.cell_value(0, i) == columnName):14 columnIndex = i15 break16 return columnIndex17 def readExcelDataByName(fileName, sheetName):18 #print fileName19 table = None20 errorMsg = ""21 try:22 data = xlrd.open_workbook(fileName)23 table = data.sheet_by_name(sheetName)24 except Exception, msg:25 errorMsg = msg26 return table, errorMsg27 def readExcelDataByIndex(fileName, sheetIndex):28 table = None29 errorMsg = ""30 try:31 data = xlrd.open_workbook(fileName)32 table = data.sheet_by_index(sheetIndex)33 except Exception, msg:34 errorMsg = msg35 return table, errorMsg36 if __name__ == '__main__':37 #example38 xlsfile= 'F:/test_AutoTesting/TestCase/RunList.xlsx'39 table = readExcelDataByName(xlsfile, 'Sheet1')[0]40 #获取第一行的值 41 testcase_id = table.cell_value(1, getColumnIndex(table,'TestCaseID'))42 app_config = table.cell_value(1, getColumnIndex(table,'APPConfig'))43 44 print u'测试用例ID为:%s'%(testcase_id)45 print u'配置信息为:%s'%(app_config)