python读取excel表格

自用,来源于python读取excel中的数据_zijikanwa的博客-CSDN博客_python读取excel数据
import xlrd  # xlrd是读取excel表
#import xlwt #w是写

def read_excel():
    # 打开文件
    workBook = xlrd.open_workbook(r'C:\...\Desktop\first.xlsx')

    # 1.获取sheet的名字
    # 1.1 获取所有sheet的名字(list类型)
    allSheetNames = workBook.sheet_names()
    print(allSheetNames)

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

    # 2. 获取sheet内容
    ## 2.1 法1:按索引号获取sheet内容
    sheet1_content1 = workBook.sheet_by_index(0)  # sheet索引从0开始
    ## 2.2 法2:按sheet名字获取sheet内容
    sheet1_content2 = workBook.sheet_by_name('Sheet1')

    # 3. sheet的名称,行数,列数
    print(sheet1_content1.name,sheet1_content1.nrows,sheet1_content1.ncols)

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

    # 5. 获取单元格内容(三种方式)
    print(sheet1_content1.cell(1, 0).value)
    print(sheet1_content1.cell_value(2, 2))
    print(sheet1_content1.row(2)[2].value)

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


if __name__ == '__main__':
    read_excel()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值