zoukankan      html  css  js  c++  java
  • 读写excel文件

    读文件

    import xlrd
    
    book = xlrd.open_workbook('学生名单.xlsx')#打开文档
    sheet1 = book.sheet_by_name('作业')#打开第几页
    
    print(sheet1.row_values(1))#获取指定的第几行的数据
    print(sheet1.col_values(1))#获取某指定的一列的数据
    print(sheet1.cell(0,0).value)#获取指定的单元格的数据
    print(sheet1.nrows)#多少行
    print(sheet1.ncols)#多少列

    写文件

    import xlwt
    
    book = xlwt.Workbook()#新建一个文件并写入
    sheet = book.add_sheet('人员名单')
    sheet.write(0,0,'姓名')
    sheet.write(0,1,'学习形式')
    sheet.write(1,0,'xxx')
    sheet.write(1,1,'zzz')
    
    book.save('student.xlsx')#保持的时候,如果用的是office,后缀.xls

    枚举函数

    for index,value in enumerate(l):#enumerate为枚举函数,枚举l中的每个元素,index取的角标
        print('编号%s --> %s'%(index,value))

    修改excel文档

    from xlutils import copy
    import xlrd
    
    #1、打开文档
    #2、复制一份
    #3、修改
    
    book = xlrd.open_workbook('student.xlsx')
    new_book = copy.copy(book)#复制一份
    sheet = new_book.get_sheet(0)#获取sheet页
    title = ['编号','名字','地址','电话','性别']
    
    for col,t in enumerate(title):
        sheet.write(0,col,t)
    
    new_book.save('student.xlsx')
  • 相关阅读:
    学习进度
    作业8:单元测试练习
    用户体验设计案例分析
    团队协作一
    需求分析
    结对编程——词频统计 2
    结对编程——词频统计
    个人项目-词频统计
    数组求和
    个人介绍和Github使用流程
  • 原文地址:https://www.cnblogs.com/rj-tsl/p/11851326.html
Copyright © 2011-2022 走看看