zoukankan      html  css  js  c++  java
  • 操作excel--xlwt/xlrd/xlutils模块

    一.写Excel (导入xlwt模块)
    需求:只要你传入一个表名,就能把所有的数据导入出来写入excel,字段名是excel的表头
    分析:
      1、要动态获取到表的字段 cur.description能获取到表的字段
      fileds = [ filed[0] for filed in cur.description ] #列表生成式写法(其他章节有专门介绍)
      2、获取数据 select * from "%s" % table_name
      3、循环写入excel
    代码及解析如下:
    import pymysql,xlwt
    def export_excel(table_name):
    host, user, passwd, db = '118.24.3.xx', 'jxzx', '123456', 'jxzx'
    coon = pymysql.connect(user=user, host=host, port=3306, passwd=passwd, db=db, charset='utf8')
    cur = coon.cursor()
    sql = 'select * from %s;'%table_name
    cur.execute(sql) # 执行sql
    fileds = [filed[0] for filed in cur.description] #所有的字段(表头)
    all_data = cur.fetchall()
    book = xlwt.Workbook() #创建excel文件
    sheet = book.add_sheet('sheet1')
    # col = 0
    # for filed in fileds: #循环写表头
    # sheet.write(0,col,filed)
    # col+=1
    for col,filed in enumerate(fileds): #循环写表头,其中enumerate方法可以自动使col自动+1循环写,等同于上面注释掉的四行内容写法
    sheet.write(0,col,filed)
    row = 1 #行数,从第一行开始,第0行已经被表头写入
    # print(all_data) #查看所有的数据及格式,是二维数组
    # ((1, '小黑马', '男', 28, '河南省济源市北海大道32号', '天蝎座', '18002531114', 617741546),
    # (1, '小黑马', '男', 28, '河南省济源市北海大道32号', '天蝎座', '18002531114', 617741546),
    for data in all_data: #二维数组,控制行
    for col,filed in enumerate(data): #控制列
    sheet.write(row,col,filed)
    row+=1 #每次写完一行,行就加1
    book.save('%s.xls'%table_name)
    export_excel('app_student')

    部分结果如下图:

    二.读Excel    (导入xlrd模块)

    book = xlrd.open_workbook('app_student.xls')  #打开excel
    sheet = book.sheet_by_index(0) #根据顺序获取excel sheet页
    sheet = book.sheet_by_name('sheet1') #根据名字获取excel sheet页
    print(sheet.cell(0,0).value) #指定sheet页里面行和列获取数据
    print(sheet.row_values(0)) #获取到第几行的内容,以列表形式打印。0行即表头
    print(sheet.row_values(1)) #获取第一行的内容
    print(sheet.nrows) #获取到excel里面总共有多少行
    for i in range(sheet.nrows): #循环获取每行的数据
    print(sheet.row_values(i))
    print(sheet.ncols) #获取到excel里共有多少列
    print(sheet.col_values(0)) #获取第几列的数据
    结果如下:

    三.修改Excel    (导入xlutils模块)

    修改excel时,必须先读出来,复制一份后再修改内容。

    import xlrd
    from xlutils import copy #导入xlutils模块里的copy方法
    book = xlrd.open_workbook('app_student.xls') #先用xlrd模块,打开一个excel
    new_book = copy.copy(book) #通过xlutils这个模块里面copy方法,复制一份excel
    sheet = new_book.get_sheet(0) #获取新copy的excel的sheet页,get_sheet()获取sheet页的方法
    # sheet.write(0,0,'编号') #修改第0行第0列的表头为“编号”
    # sheet.write(0,1,'名字') #修改第0行第1列的表头为“名字”

    lis = ['编号','名字','性别','年龄','地址','班级','手机号','金币'] #举例一个列表,实现循环修改每个字段
    for col,filed in enumerate(lis): #循环修改表头list的字段
    sheet.write(0,col,filed)
    new_book.save('app_student.xls')
    将表头修改为lis的内容,如下结果:



    
    


  • 相关阅读:
    《父亲写的散文诗》--许飞
    python 解数独
    github key already in use
    openwrt ddns绑定域名
    hexo 长期后台运行
    修复云服务器rpm无法使用的问题
    vim 取消筛选高亮
    力扣 2021.02.25 最长公共前缀
    [模板]-Manacher
    背包问题回顾
  • 原文地址:https://www.cnblogs.com/fancyl/p/9008087.html
Copyright © 2011-2022 走看看