python xlrd读取execl数据

1 使用xlrd模块,读取工作表中的数据(需要安装xlrd模块)
file
="x.xlsx" book=xlrd.open_workbook(file) #打开某个工作簿,即一个execl文件 sheetnames=book.sheet_names() #获取execl中的所有的sheet表名 print(sheetnames) --》['Sheet1', 'Sheet2', 'Sheet3'] for i in range(len(sheetnames)): #遍历每个表 sheet=sheetnames[i] print (sheet) --》Sheet1 sheet=book.sheet_by_name('Sheet1') # 读取execl中的工作表Sheet1 cell=sheet.cell(0,1) #读取某个(第1行,第2列)单元格的内容 print (sheet.nrows) #行数 print (sheet.ncols) #列数 #遍历工作表Sheet1中的每个数据: data = [] for line in range(sheet.nrows): cln = [] for col in range(sheet.ncols): celldata=sheet.cell(line,col).value cln.append(celldata) #print (cln) data.append(cln) print (data) import xlrd #mport openpyxl from xlrd import open_workbook file="x.xlsx" book=xlrd.open_workbook(file) sheet=book.sheet_by_name('Sheet1') cell=sheet.cell(0,0) print (sheet.nrows) #行数 print (sheet.ncols) #列数 data = [] for line in range(sheet.nrows): ln = [] for col in range(sheet.ncols): cda=sheet.cell(line,col).value ln.append(cda) data.append(ln) print (data)

 

posted @ 2022-07-24 20:45  Sky-wings  阅读(163)  评论(0编辑  收藏  举报