py拆分excel,没有wps莫慌
  6DMaaPzJglxt 2023年12月05日 26 0



python拆分工作表

  • xlrd库
  • xlwt库
  • 拆分excel


xlrd库

读取execl

  • sheet=open_workbook(文件名) 打开excel文件
  • sheet.sheet_names(name) 获取工作表的工作表名列表
  • sheet.sheet_by_index(index) 根据索引获取工作表
  • sheet.sheet_by_name(名字) 根据名字获取工作表
  • sheet.sheets 所有的工作表对象
  • sheet.nrows 行
  • sheet.ncols 列

cell属性

  • sheet.cell(row,col) 指定行列的cell对象(cell对象包含数据类型和内容)
  • sheet.row_slice(row,start_col,end_col) 指定行的某几列cell对象
  • sheet.col_slice(col,start_row,end_roe) 指定列的某几行cell对象
  • sheet.cell_value(row,col) 指定行列的值
  • sheet.row_values(row,start_col,end_col) 指定行的某几列的值
  • sheet.col_values(col,start_row,end_roe) 指定列的某几行的值

xlwt库

写入excel

  • workbook=xlwt.Workbook()
  • sheet=workbook.add_sheet(name) 添加name工作表
  • sheet.write(row,col,value) 指定行列的表格写入value
  • workbook.save(name.xls) 保存

拆分excel

import xlrd,xlwt
def split_ecel(excel_name):
    try:
        with xlrd.open_workbook(excel_name) as file:
            print(file.sheet_names())  # execl名字列表
            print(file.sheet_by_index(0), file.sheet_by_name(file.sheet_names()[0]))  # 根据索引和name获取
            for f in file:
                name = f.name  # 工作表名
                rows = f.nrows  # 行数
                cols = f.ncols  # 列数
                print(name, rows, cols)
                cell = f.cell(0, 0)  # 第一行第一列的表格 包括数据类型和内容 .value
                print(type(cell), cell, cell.value)  # 读取信息
                workbook = xlwt.Workbook()  #
                sheet = workbook.add_sheet(name)  # 添加一个工作表
                for r in range(0, rows):
                    for c in range(0, cols):
                        value = f.cell(r, c).value  # 取出数据
                        sheet.write(r, c, value)  # 写入
                workbook.save(name + '.xls')  # 保存
    except Exception as e:
        print(e)
        return False
    return True
print("input excel name to split:")
name=str(input())
split_ecel(name)

py拆分excel,没有wps莫慌_文件名


拆分成功!

py拆分excel,没有wps莫慌_表名_02


【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月05日 0

暂无评论

推荐阅读
6DMaaPzJglxt