【使用xlrd、xlutils读写excel】

1、环境和版本

  • 电脑:win
  • excel文件格式.xls
  • xlrd 版本 1.2.0(高版本存在部分属性不兼容),安装指定版本pip install xlrd == 1.2.0
  • xlutils 版本:2.0.0

2、相关使用方法封装

2.1 根据行下标返回对应行数据

def read_excel_by_row(file_path, sheet_index, row_index):
    """读取指定行数据"""
    data = xlrd.open_workbook(file_path)
    sheet = data.sheet_by_index(sheet_index)
    nrows = sheet.nrows  # 行数
    ncols = sheet.ncols  # 列数
    try:
        table_list = sheet.row_values(rowx=row_index, start_colx=0, end_colx=None)  # start_colx:开始列,end_colx:结束列
        return table_list
    except Exception as e:
        print("当前表单行数%s行%s列" % (nrows, ncols), e)

2.2 根据列下标返回对应列数据

def read_excel_by_col(file_path, sheet_index, col_index):
    """读取指定列数据"""
    data = xlrd.open_workbook(file_path)
    sheet = data.sheet_by_index(sheet_index)
    nrows = sheet.nrows  # 行数
    ncols = sheet.ncols  # 列数
    try:
        table_list = sheet.col_values(colx=col_index
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值