1.python 读取Excel文件(.xls文件)
导入的为xlrd
读取该sheet表中的值
import xlrd
# 读取表格
def read_excel(book_data,sheet_data):
data_path=r"XXX"(XXX为Excel表格的路径)
file=data_path+"/"+book_data+".xls"
# 打开文件
workbook=xlrd.open_workbook(file)
# 读取sheet页
sheet=workbook.sheet_by_name(sheet_data)
# 获取表的行列数
rows=sheet.nrows
cols=sheet.ncols
print(rows)
print(cols)
all_list=list()
# 获取表中数值
for row in range(1,rows):
tempList=[]
for col in range(0,cols):
tempList.append(sheet.cell(row,col).value)
all_list.append(tempList)
print(all_list)
return all_list
if __name__ == '__main__':
order_allList = read_excel("Excel文件名", "sheet页名")
运行结果:
3
17
[['禹亦丽', '384', '一为光羽学校', '13382327441', '112', '0', '71232784893452035284681', 2.0, 17600.0, 17600.0, 1.0, 620.0, 121.0, '二年级 2班', '42', '1', '17'], ['薛梅菊', '384', '一为光羽学校', '18454670091', '164', '1', '71237899076089285133273', 2.0, 26400.0, 26400.0, 1.0, 620.0, 121.0, '一年级 6班', '28', '2', '30']]
2.python读取Excel表(.xlsx文件)
导入的为openpyxl
import openpyxl
# 读取Excel表格
def read_excel(book_data,sheet_data):
data_path=r"XXX"(XXX为Excel表格的路径)
file=data_path+"/"+book_data+".xlsx"
# 打开文件
workbook=openpyxl.load_workbook(file)
# 读取sheet页
sheet=workbook[sheet_data]
allList=list()
for row in range(2,sheet.max_row+1):
for column in range(1,sheet.max_column+1):
allList.append(sheet.cell(row,column).value)
return allList
if __name__ == '__main__':
order_allList = read_excel("Excel文件名", "sheet页名")