安装xlrd模块
代码
import xlrd
class ExcelUtill():
def __init__(self,excelpath,sheetName="Sheet1"):
self.data = xlrd.open_workbook(excelpath)
self.table = self.data.sheet_by_name(sheetName)
self.keys = self.table.row_values(0)
self.rowNum = self.table.nrows
self.colNum = self.table.ncols
def dict_data(self):
if self.rowNum <= 1:
print("总行数小于1")
else:
r = []
j = 1
for i in range(self.rowNum-1):
s={}
values = self.table.row_values(j)
for x in range(self.colNum):
s[self.keys[x]] = values[x]
r.append(s)
j+=1
return r
if __name__=="__main__":
filepath = r"工作簿1.xls"
data1 = ExcelUtill(filepath)
data = data1.dict_data()
print(data)