|
导入引用
import xlrd import xlwt inExcel = r"C:\Users\Administrator\Desktop\13456.xls"
第一步:读取Excel表的sheet
# 打开Excel文件 dataExcel = xlrd.open_workbook(inExcel) print (dataExcel.sheet_names()) #获取sheet名称 print dataExcel.sheet_names() #获取sheet名称 print dataExcel.nsheets # 获取sheet数目
第二步:读取具体的sheet
table01 = dataExcel.sheet_by_index(0) #获取sheet print table01.ncols #获取sheet列数 print table01.nrows #获取sheet行数
第三步:读取具体的数值
for i in range(1, table01.nrows): for j in range(table01.ncols): cellType = table01.cell(i, j).ctype # 获取Excel的值的类型 cellValue = table01.cell_value(i, j) # 获取Excel的值
具体的示例:
# coding:utf-8 import xlrd inExcel = r"C:\Users\Administrator\Desktop\13456.xls" # 打开Excel文件 dataExcel = xlrd.open_workbook(inExcel) print dataExcel.sheet_names() #获取sheet名称 table01 = dataExcel.sheet_by_index(0) row_content = [] # 初始化一个列表,用来存储Excel的值 for i in range(1, table01.nrows): for j in range(table01.ncols): cellType = table01.cell(i, j).ctype # 获取Excel的值的类型 cellValue = table01.cell_value(i, j) # 获取Excel的值 row_content.append(cellValue) print row_content
参考:
https://www.cnblogs.com/feiyueNotes/p/7786579.html
https://www.cnblogs.com/lwf1990/p/7755192.html
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-4-27 01:34
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社