使用python处理excle表格

# -*- coding: utf-8 -*-

import xlrd
###########################
#通用功能,读取excel表格中所有数据
#返回一个包含所有单元格名和对应值的字典
###########################

#读取excel表格
def read_excel(fileFullPath,sheetname,colNameTag=0):
    workbook = xlrd.open_workbook(fileFullPath)     #实例化一个对象
    table = workbook.sheet_by_name(sheetname)   #根据sheetname获取目标表单
    rowNum = table.nrows  #获取行数
    colNum = table.ncols
    #print("rowNum=%s,colNum=%s"%(rowNum,colNum))
    resultDic = {}
    for row in range(rowNum):       #循环读取每个单元格位置和值并存到字典
        for col in range(colNum):
            resultDic[xlrd.cellname(row,col)]=(table.cell(row,col).value)
            #print(resultDic)
            #print(table.cell(row,col).value)
    return resultDic

#基于read_excel(),输入字典、单元格位置获取对应的值
def select_from_dic(dic,cellPosition):
    if cellPosition in dic.keys():
        value = dic[cellPosition]
        return value
    else:
        print("no this cellPositon:",cellPosition)

#测试
data = read_excel("F:\Test\(testV10.xlsx","Buglist") 
result = select_from_dic(data,"E7")   
print(result)
    

测试表格:

执行结果:

陈月白 http://www.cnblogs.com/chenyuebai
原文地址:https://www.cnblogs.com/chenyuebai/p/5612538.html