zoukankan      html  css  js  c++  java
  • day7-Python学习笔记(十六)excel操作

    import xlwt #写入excel
    # book = xlwt.Workbook() #新建一个excel
    # sheet = book.add_sheet('sheet1') #添加一个sheet页
    # sheet.write(0,0,'姓名')
    # sheet.write(0,1,'性别')
    # sheet.write(0,2,'年龄')
    # book.save('stu.xls') #微软的office不能用xlsx结尾的,wps随意


    stus = [
    ['姓名','年龄','性别','分数'],
    ['mary', 20, '女', 89.9],
    ['mary', 20, '女', 89.9],
    ['mary', 20, '女', 89.9],
    ['mary', 20, '女', 89.9]
    ]
    book = xlwt.Workbook() #新建一个excel
    sheet = book.add_sheet('sheet1') #添加一个sheet页
    raw = 0#控制行的
    for stu in stus:
    cols = 0 #控制列
    for s in stu:
    sheet.write(raw,cols,s)
    cols+=1
    raw+=1

    book.save('kkk.xls')

    import xlrd#读取excel
    book = xlrd.open_workbook('stu.xls') #打开一个excel
    sheet = book.sheet_by_index(0) #根据顺序获取sheet
    # sheet2 = book.sheet_by_name('sheet1') #根据sheet页名字获取sheet
    # print(sheet.cell(0,0).value) #指定行和列获取数据
    # print(sheet.ncols) #获取excel里面有多少列
    # print(sheet.nrows) #获取excel里面有多少行
    sheet.row_values(1)#取第几行的数据
    print(sheet.col_values(1)) #取第几列的数据
    for i in range(sheet.nrows): # 0 1 2 3 4 5
    print(sheet.row_values(i)) #取第几行的数据


    from xlutils.copy import copy  #修改excel
    import xlrd
    book1 = xlrd.open_workbook('stu.xls')
    book2 = copy(book1) #拷贝一份原来的excel
    sheet = book2.get_sheet(0) #获取第几个sheet页
    sheet.write(1,3,0)
    sheet.write(1,0,'小黑')
    book2.save('stu.xls')









  • 相关阅读:
    Luogu P4246 [SHOI2008]堵塞的交通(线段树+模拟)
    Luogu P2619 [国家集训队2]Tree I(WQS二分+最小生成树)
    Luogu P2042 [NOI2005]维护数列(平衡树)
    Luogu P1052 过河(dp)
    Luogu P1041 传染病控制(搜索)
    Luogu P2717 寒假作业(平衡树)
    Luogu P2822 组合数问题(前缀和)
    Luogu P2827 蚯蚓(模拟)
    随机图片测试
    Luogu P2458 [SDOI2006]保安站岗(树形dp)
  • 原文地址:https://www.cnblogs.com/flynn0825/p/8407329.html
Copyright © 2011-2022 走看看