python+selenium+new——xlrd库——读取excel文件——xls结尾为示例 ——数据格式

from datetime import date, datetime

import xlrd


# 单元格类型:  0. empty(空的),1 string(text), 2 number, 3 date, 4 boolean, 5 error, 6 blank(空白表格)

book = xlrd.open_workbook("C:\\Users\\del\\Desktop\\Book2.xls")


print(book.nsheets)          #返回sheet的数量,此处返回 3

print(book.sheet_names())   #返回所有sheet名称;['党费', 'Sheet2', 'Sheet3']





sheet_name = book.sheet_names()[0]  # 获取指定索引的sheet的名字
print(sheet_name)  # Sheet1

# sheet0 = book.sheet_by_index(0)  #通过sheet索引获取sheet对象

sheet0 = book.sheet_by_name("党费")

print(sheet0.nrows)  # 总行数:2
print(sheet0.ncols)  # 总列数:6

print(sheet0.row_values(0))  # 获取第一行数据:['身份证号', '姓名', '学号', '缴费项目', '缴费金额', '出生日期']
print(sheet0.row_values(1))  # 获取第二行数据:['33038119890412221X', '潘颂哲', '33038119890412221X', '2020年2月份党费', 14.0, 32610.0]

print(sheet0.col_values(0))  # 获取第一列的数据:['身份证号', '33038119890412221X']
print(sheet0.col_values(1))  # 获取第二列的数据:['姓名', '潘颂哲']
print(sheet0.col_values(2))  # 获取第三列的数据:['学号', '33038119890412221X']
print(sheet0.col_values(3))  # 获取第四列的数据:['缴费项目', '2020年2月份党费']
print(sheet0.col_values(4))  # 获取第五列的数据:['缴费金额', 14.0]
print(sheet0.col_values(5))  # 获取第六列的数据:['出生日期', 32610.0]

# 通过cell的位置坐标获取指定cell的值:

print(sheet0.cell_value(0, 0))  # 获取第一行第一列的数据:身份证号
print(sheet0.cell_value(0, 1))  # 获取第一行第二列的数据:姓名

print(sheet0.cell_value(1, 4))  # 获取第二行第五列的数据:14.0
print(sheet0.cell(1,4).value)  # 获取第二行第五列的数据:14.0

print(sheet0.cell(1,5).value)  # 获取第二行第五列的数据:32610.0
#----------------------------------------

# 数据类型  0 empty,

#   1 string,

#   2 number,

#   3 date,

#   4 boolean,

#   5 error

#------------------------------------------------

print(sheet0.cell(1,4).ctype)  #返回2 ,说明是number类型


print(type(sheet0.cell(1,4).value))  #返回<class 'float'>

print(int(sheet0.cell(1,4).value))   #返回14
print(type(int(sheet0.cell(1,4).value)))  #返回<class 'int'>

#-----------------------------------------------------------------------------

print(sheet0.cell(1,5).ctype)  #返回3,说明是date类型


print(xlrd.xldate_as_tuple(sheet0.cell_value(1,5),book.datemode))   #返回(1989, 4, 12, 0, 0, 0)

date_value = xlrd.xldate_as_tuple(sheet0.cell_value(1,5),book.datemode)

print(date(*date_value[:3]))   #返回 1989-04-12

print(date(*date_value[:3]).strftime('%Y/%m/%d'))   #返回:1989/04/12

print(date(*date_value[:3]).strftime('%Y-%m-%d'))   #返回:1989-04-12


#--------------------------------------------------------------------------------

print(sheet0.row(0))      #获取指定行,返回cell对象的列表   同时展示行中各cell对象的数据类型[text:'身份证号', text:'姓名', text:'学号', text:'缴费项目', text:'缴费金额', text:'出生日期']

print(sheet0.row_values(0) )  #获取指定行,返回列表['身份证号', '姓名', '学号', '缴费项目', '缴费金额', '出生日期']

print(sheet0.row(1)) #[text:'33038119890412221X', text:'潘颂哲', text:'33038119890412221X', text:'2020年2月份党费', number:14.0, xldate:32610.0]





print(sheet0.col(0))          #获取指定列,返回cell对象的列表   同时展示列中各cell对象的数据类型 [text:'身份证号', text:'33038119890412221X']

print(sheet0.col_values(0) )  #获取指定列,返回列表   列中的内容['身份证号', '33038119890412221X']

执行结果:

3
['党费', 'Sheet2', 'Sheet3']
党费
2
6
['身份证号', '姓名', '学号', '缴费项目', '缴费金额', '出生日期']
['33038119890412221X', '潘颂哲', '33038119890412221X', '2020年2月份党费', 14.0, 32610.0]
['身份证号', '33038119890412221X']
['姓名', '潘颂哲']
['学号', '33038119890412221X']
['缴费项目', '2020年2月份党费']
['缴费金额', 14.0]
['出生日期', 32610.0]
身份证号
姓名
14.0
14.0
32610.0
2
<class 'float'>
14
<class 'int'>
3
(1989, 4, 12, 0, 0, 0)
1989-04-12
1989/04/12
1989-04-12
[text:'身份证号', text:'姓名', text:'学号', text:'缴费项目', text:'缴费金额', text:'出生日期']
['身份证号', '姓名', '学号', '缴费项目', '缴费金额', '出生日期']
[text:'33038119890412221X', text:'潘颂哲', text:'33038119890412221X', text:'2020年2月份党费', number:14.0, xldate:32610.0]
[text:'身份证号', text:'33038119890412221X']
['身份证号', '33038119890412221X']

猜你喜欢

转载自www.cnblogs.com/xiaobaibailongma/p/12323152.html