python读取excel

import xlrd
#xlrd2.0版本只能读取xls
#xlrd1.0.2版本能读取xlsx

def read_excel():
    # 打开文件
    workBook = xlrd.open_workbook(r'  ');#excel文件路径

    # 1.2 按索引号获取sheet的名字(string类型)
    # sheet1Name = workBook.sheet_names()[0];
    # print(sheet1Name);

    # 2. 获取sheet内容
    sheet1_content1 = workBook.sheet_by_index(0); # sheet索引从0开始
    # # 按sheet名字获取sheet内容,sheet的名称,行数,列数
    # print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols);

    # 4. 获取整行和整列的值(数组)
    rows = sheet1_content1.row_values(3); # 获取第四行内容
    cols = sheet1_content1.col_values(1); # 获取第二列内容
    print(rows);
    print(cols);

    # 5. 获取单元格内容(三种方式)
    # print(sheet1_content1.cell(1, 0).value);
    # print(sheet1_content1.cell_value(1, 1));
    # print(sheet1_content1.row(1)[1].value);
    #
    # # 6. 获取单元格内容的数据类型
    # # Tips: python读取excel中单元格的内容返回的有5种类型 [0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error]
    # print(sheet1_content1.cell(0, 0).ctype);


if __name__ == '__main__':
    read_excel();

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值