自动化处理--python读取表格数据xlrd

 

目录

1.安装

2 打开&关闭

 3 sheet操作

4 行、列操作

 5 单元格操作

 6 其他

A 附件-汇总


1.安装

安装 xlrd 模块 pip install xlrd

注意选择版本,高版本可能只支持xlsx

2 打开&关闭

workBook = xlrd.open_workbook(r"D:\AAtocode\PY\ex1.xlsx"); 

 workBook.release_resources() #释放打开的表格

 3 sheet操作

 获取sheet名称、数量;

allSheetNames = workBook.sheet_names();
print('打印全部的sheet名称',allSheetNames);
sheet_num = len(allSheetNames)
print('该表格sheet共有 :',sheet_num ,'个')
for i in range(sheet_num) :
    print('第{}个sheet名称为'.format(i+1),allSheetNames[i]);
# # 1.2 按索引号获取sheet的名字(string类型)
sheet1Name = workBook.sheet_names()[0];
# print(sheet1Name);
sheet2Name = workBook.sheet_names()[1];
# print(sheet2Name);

确认sheet加载情况,返回布尔值

#查某个sheet是否导入完毕 workBook.sheet_loaded(sheet_name or indx) 
print("第二个sheet是否导入完毕?", workBook.sheet_loaded(1))

读取sheet内容,获取行列数

# ## 2.1 法1:按索引号获取sheet内容
sheet1_content1 = workBook.sheet_by_index(0); # sheet索引从0开始
sheet1_content3 = workBook.sheets()[0]
    # ## 2.2 法2:按sheet名字获取sheet内容
sheet1_content2 = workBook.sheet_by_name('Sheet1');
    #sheet 的行数
# sheet1_rows = sheet1_content1.nrows;
# print(sheet1_rows)
for i in range(sheet_num) :
    sheet_content = workBook.sheet_by_index(i); # sheet索引
    # # 3. sheet的名称,行数,列数
    print('第{}个sheet名称为'.format(1+i),sheet_content.name,'有',sheet_content.nrows,'行',sheet_content.ncols,'列');

4 行、列操作

获取全行数据、切片、全行类型、行的有效单元格数等

# # 4. 获取整行和整列的值(数组)
k = 2
rows = sheet1_content1.row_values(k); # 获取第3行内容  row_values(rowx=2, start_colx=0, end_colx=None)
cols = sheet1_content1.col_values(k); # 获取第3列内容

###   行操作 对应列操作把 row 换成 col
print(sheet1Name,'第',k+1,'行内容为',rows) 
print(sheet1_content1.row_slice(rowx=k))#返回由该行中所有的单元格对象类型及对象组成的列表
print(sheet1_content1.row_types(rowx=k, start_colx=0, end_colx=None))#返回由该行中所有的单元格对象类型及对象组成的列表
print(sheet1_content1.row(rowx=k))#返回由该行中所有的单元格对象类型及对象组成的列表
print(sheet1_content1.row_len(rowx=k)) #返回该行的有效单元格长度
    # # 5. 获取单元格内容(三种方式)
print(sheet1_content1.cell(0, 1).value)
print(sheet1_content1.cell_value(0, 1))
print(sheet1_content1.row(0)[1].value)
# print(sheet1_content1.row(10)[10].value) ## 超出索引长度会报错

 5 单元格操作

获取单元格数据、类型

# # 5. 获取单元格内容(三种方式)
print(sheet1_content1.cell(0, 1).value)
print(sheet1_content1.cell_value(0, 1))
print(sheet1_content1.row(0)[1].value)
# print(sheet1_content1.row(10)[10].value) ## 超出索引长度会报错
    # # 6. 获取单元格内容的数据类型
    # # Tips: python读取excel中单元格的内容返回的有5种类型 
    # #[0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error,6 blank(空白表格)]
print(sheet1_content1.cell(0, 1).value,' 属于',sheet1_content1.cell(0, 1).ctype)
print(sheet1_content1.cell(1, 1).value,' 属于',sheet1_content1.cell(1, 1).ctype)
print(sheet1_content1.cell(2, 3).value,' 属于',sheet1_content1.cell(2, 3).ctype)

 6 其他

print(xlrd.cellname(1,1))
print(xlrd.cellnameabs(0,0))
print(xlrd.colname(0))

A 附件-汇总

import xlrd
import xlwt
# import pyexcel-xls #这个也要安装
# 打开文件
workBook = xlrd.open_workbook(r"D:\AAtocode\PY\ex1.xlsx");

    # # 1.获取sheet的名字
    # # 1.1 获取所有sheet的名字(list类型)
allSheetNames = workBook.sheet_names();
print('打印全部的sheet名称',allSheetNames);
sheet_num = len(allSheetNames)
print('该表格sheet共有 :',sheet_num ,'个')
for i in range(sheet_num) :
    print('第{}个sheet名称为'.format(i+1),allSheetNames[i]);
# # 1.2 按索引号获取sheet的名字(string类型)
sheet1Name = workBook.sheet_names()[0];
# print(sheet1Name);
sheet2Name = workBook.sheet_names()[1];
# print(sheet2Name);

#查某个sheet是否导入完毕 workBook.sheet_loaded(sheet_name or indx) 
print("第二个sheet是否导入完毕?", workBook.sheet_loaded(1))
    # # 2. 获取sheet内容
    # ## 2.1 法1:按索引号获取sheet内容
sheet1_content1 = workBook.sheet_by_index(0); # sheet索引从0开始
sheet1_content3 = workBook.sheets()[0]
    # ## 2.2 法2:按sheet名字获取sheet内容
sheet1_content2 = workBook.sheet_by_name('Sheet1');
    #sheet 的行数
# sheet1_rows = sheet1_content1.nrows;
# print(sheet1_rows)
for i in range(sheet_num) :
    sheet_content = workBook.sheet_by_index(i); # sheet索引
    # # 3. sheet的名称,行数,列数
    print('第{}个sheet名称为'.format(1+i),sheet_content.name,'有',sheet_content.nrows,'行',sheet_content.ncols,'列');

# # 4. 获取整行和整列的值(数组)
k = 2
rows = sheet1_content1.row_values(k); # 获取第3行内容  row_values(rowx=2, start_colx=0, end_colx=None)
cols = sheet1_content1.col_values(k); # 获取第3列内容

###   行操作 对应列操作把 row 换成 col
print(sheet1Name,'第',k+1,'行内容为',rows) 
print(sheet1_content1.row_slice(rowx=k))#返回由该行中所有的单元格对象类型及对象组成的列表
print(sheet1_content1.row_types(rowx=k, start_colx=0, end_colx=None))#返回由该行中所有的单元格对象类型及对象组成的列表
print(sheet1_content1.row(rowx=k))#返回由该行中所有的单元格对象类型及对象组成的列表
print(sheet1_content1.row_len(rowx=k)) #返回该行的有效单元格长度
    # # 5. 获取单元格内容(三种方式)
print(sheet1_content1.cell(0, 1).value)
print(sheet1_content1.cell_value(0, 1))
print(sheet1_content1.row(0)[1].value)
# print(sheet1_content1.row(10)[10].value) ## 超出索引长度会报错
    # # 6. 获取单元格内容的数据类型
    # # Tips: python读取excel中单元格的内容返回的有5种类型 
    # #[0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error,6 blank(空白表格)]
print(sheet1_content1.cell(0, 1).value,' 属于',sheet1_content1.cell(0, 1).ctype)
print(sheet1_content1.cell(1, 1).value,' 属于',sheet1_content1.cell(1, 1).ctype)
print(sheet1_content1.cell(2, 3).value,' 属于',sheet1_content1.cell(2, 3).ctype)

print(xlrd.cellname(1,1))
print(xlrd.cellnameabs(0,0))
print(xlrd.colname(0))
workBook.release_resources() #释放打开的表格

 表格共有两个sheet;

第一个sheet:(名称为sheet1)

ABCD
1
2123
34hellohi
BBQ

第二个sheet,名称为: 第二sheet

 输出如下:

打印全部的sheet名称 ['Sheet1', '第二sheet']
该表格sheet共有 : 2 个
第1个sheet名称为 Sheet1
第2个sheet名称为 第二sheet
第二个sheet是否导入完毕? True
第1个sheet名称为 Sheet1 有 3 行 4 列
第2个sheet名称为 第二sheet 有 3 行 3 列
Sheet1 第 3 行内容为 ['', 4.0, 'hello', 'hi\nBBQ']
[empty:'', number:4.0, text:'hello', text:'hi\nBBQ']
array('B', [0, 2, 1, 1])
[empty:'', number:4.0, text:'hello', text:'hi\nBBQ']
4
姓名
姓名
姓名
姓名  属于 1
1.0  属于 2
hi
BBQ  属于 1
B2
$A$1
A

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值