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')
  • 相关阅读:
    Jetson AGX Xavier初始化
    Jetson AGX Xavier更换apt-get源
    Mac远程访问Ubuntu
    Anaconda安装和环境的搭建
    C# NotifyIcon 托盘控件
    VS2010+OpenMP的简单使用
    VS2010+OpenCV3.4.1+zbar 64位
    datatbales 使用笔记
    ubuntu16.04 常用软件
    crontab不能正常执行的五种原因
  • 原文地址:https://www.cnblogs.com/rj-tsl/p/11851326.html
Copyright © 2011-2022 走看看