zoukankan      html  css  js  c++  java
  • 读excel和openpyxl模块

    import xlrd
    book = xlrd.open_workbook('ssj.xls') # 打开某个文件
    sheet = book.sheet_by_index(0) # 通过sheet页的下标打开
    # sheet = book.sheet_by_name('sheet10') # 通过sheet页的名字打开
    # res = sheet.cell(0, 0).value # 获取某个单元格的内容
    res = sheet.row_values(0) # 获取某一行的内容
    res2 = sheet.col_values(0) # 获取某一列的内容
    # sheet.nrows # 所有行
    # sheet.ncols # 所有列
    for row in range(sheet.nrows): # 按照行循环打出文件的所有内容
    print(sheet.row_values(row))

    写一个函数,传入一个表名,然后把这个表里的所有数据,导出到excel里面,代码如下:

    import pymysql
    import xlwt


    def con_mysql(sql):
    conn = pymysql.connect(host='118.24.3.40', user='jxz', password='123456', db='jxz', charset='utf8')
    cur = conn.cursor(pymysql.cursors.DictCursor)
    cur.execute(sql)
    res = cur.fetchall()
    cur.close()
    conn.close()
    return res


    def export_excel(table_name):
    sql = 'select * from %s;' % table_name # 查询table_name,有返回值
    res = con_mysql(sql) # 返回一个list,[{'id': 1, 'name': 'xx', 'phone':110}]
    if res:
    file_name = '%s.xls' % table_name # 用表名作为文件名,把数据写到该文件里
    book = xlwt.Workbook()
    sheet = book.add_sheet('sheet10')
    for col, v in enumerate(res[0]): # 写表头,res[0]是每个小字典,col是列,v是小字典里的key
    sheet.write(0, col, v)
    for row, row_data in enumerate(res, 1): # 从第1行开始写,跳过表头,row_data是res里的每个字典
    for col, col_data in enumerate(
    row_data.values()): # row_data.values()是dict_values([1, '13800138000', 'manu'])
    sheet.write(row, col, col_data) # col是列,col_data是字典里的每个值
    book.save(file_name)
    else:
    print('表里数据为空')
    export_excel('ssj')  # 传入表名

    openpyxl模块直接安装,pip install openpyxl,实现操作,代码如下:

    import openpyxl
    book = openpyxl.Workbook()
    sheet = book.active # 默认的sheet
    # sheet2 = book.get_sheet_by_name('sheet1') # 也可以按照这种方式来写
    sheet.append(['id', 'name', 'addr']) # append里传入一个可迭代对象
    sheet.append([1, 'sun', '北京'])
    sheet.append([2, '六三', '天津'])
    book.save('xiaohua.xls') # 保存的文件既可以是xls,也可以是xlsx格式的

    也可以指定行和列写,代码如下:

    import openpyxl
    book = openpyxl.Workbook()
    sheet = book.active # 默认的sheet
    sheet['a1'] = 'id' # 指定行和列,a1写入id
    sheet['b1'] = 'name' # 指定行和列,b1写入name
    sheet.cell(3, 2, 'hello') # 第3行,第2列写入hello,cell相当于write
    book.save('xiaohua.xls')

    openpyxl模块实现操作,代码如下:

    import openpyxl
    book = openpyxl.load_workbook('xiaohua.xlsx') # 读的时候只能是.xlsx格式的
    sheet = book.active
    print(sheet.cell(1, 1).value) # 获取第1行,第1列的内容
    print(sheet['a1'].value) # 获取第1行,第1列的内容
    print(list(sheet.rows)) # 获取所有行的数据
    print(list(sheet.columns)) # 获取所有列的数据
    # 获取所有行的数据,并保存到list中,打印出一个二维list
    lis = []
    for row in sheet.rows:
    t = []
    for col in row:
    t.append(col.value)
    lis.append(t)
    print(lis)

    openpyxl模块实现修改操作,代码如下:

    sheet.cell(3, 3, '武清')
    sheet.delete_rows(2) # 删除行
    sheet.delete_cols(1) # 删除列
    book.save('xiaohua.xlsx')

    修改完要保存一下,写和修改简单,读比较麻烦,读可以用xlrd

  • 相关阅读:
    c++函数库中一些实用的函数
    全排列
    最小生成树
    线段树初步
    各种刷题网站
    KMP初步
    【转载】在Linux系统下用dd命令制作ISO镜像U盘启动盘
    【转载】windows linux cent 7 制作U盘 启动盘
    pytho命名规范
    【转载】python中not,and,or的优先级问题及用法
  • 原文地址:https://www.cnblogs.com/laosun0204/p/11175257.html
Copyright © 2011-2022 走看看