python各种excel写入方式的速度对比

作者:左手小兜 时间:2021-04-23 22:30:15 

经过实验,新建一个excel表格,该表格拥有7个sheet,每个sheet有800条数据,其中最后一个sheet为空。

首先使用openpyxl进行写入操作,代码如下:


book = openpyxl.Workbook()
auths = Auth.objects.filter(owner_id=1)
filename = '导出数据'
for auth in auths:
 sheet = book.create_sheet(auth.name, index = 0)
 sheet.append([
     _("书名"),
     _("作者"),
     _("译者"),
     _("出版社"),
     _("序列号"),
     _("总页数"),
   ])
 objs = None
 objs = Book.objects.filter(owner_id=auth.id)
 for u in objs:
   data = []
   data.append(u.name)
   data.append(auth.name)
   data.append(u.translator)
   data.append(u.press)
   data.append(u.serializer)
   data.append(u.page)
   sheet.append(data)
return ExcelBookResponse(book, filename)


使用xlwt写入数据:


book = xlwt.Workbook()
auths = Auth.objects.filter(owner_id=1)
filename = '导出数据'
for auth in auths:
 sheet = book.add_sheet(sensor.name)
 sheet.write(0, 0, _("书名"))
 sheet.write(0, 1, _("作者"))
 sheet.write(0, 2, _("译者"))
 sheet.write(0, 3, _("出版社"))
 sheet.write(0, 4, _("序列号"))
 sheet.write(0, 5, _("总页数"))
 i = 1
 objs = None
 objs = Book.objects.filter(owner_id=auth.id)
 for u in objs:
   sheet.write(i, 0, u.name)
   sheet.write(i, 1, auth.name)
   sheet.write(i ,2,u.translator)
   sheet.write(i ,3,u.press)
   sheet.write(i, 4, u.serializer)
   sheet.write(i, 5, u.page)
   i += 1
return ExcelBookResponse(book, filename)

使用XlsxWriter写入数据:


book = xlsxwriter.Workbook(output)
auths = Auth.objects.filter(owner_id=1)
for auth in auths:
 sheet = book.add_worksheet(sensor.name)
 header = [
     _("书名"),
     _("作者"),
     _("译者"),
     _("出版社"),
     _("序列号"),
     _("总页数"),
   ]
 sheet.write_row("A1", header)
 objs = Book.objects.filter(owner_id=auth.id)
 i = 1
 for u in objs:
   sheet.write(i, 0, u.name)
   sheet.write(i, 1, auth.name)
   sheet.write(i ,2,u.translator)
   sheet.write(i ,3,u.press)
   sheet.write(i, 4, u.serializer)
   sheet.write(i, 5, u.page)
   i += 1
book.close()
file_ext = 'xlsx'
mimetype = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
# self['Content-Disposition'] = 'attachment; filename*=UTF-8\'\'"{2}.{1}"; filename="{0}.{1}"'.format(filename.replace('"', '\"'), file_ext, urllib.parse.quote(filename.replace('"', '\"'))).encode('utf8')
return HttpResponse(content=output.getvalue(), content_type=mimetype)

三者的时间比较(两种方式的文件内容是一样的):

openpyxl: 文件大小为110.75kb, 平均时间大约为570ms

xlwt: 文件大小为505.91kb,平均时间大约为440ms

XlsxWrite: 文件大小为109.28kb,平均时间大约为500ms

xlwt写入的行数有限制,因此对于较大的文件来说,XlsxWrite的速度较快一点

补充知识:python写入excel文件太慢如何解决-python往excel写入大量数据

目前用的openpyxl,从数据库获取8W行的数据通过openpyxl写入excel,要花费接近8分钟,这也太慢了,用kettle的插件秒进,python有什么方法能提升速度么,或者openpyxl能批量插入么,按行效率太低了


#!/usr/bin/python
# -*- coding: UTF-8 -*-
from openpyxl import Workbook as wbook
def xlsx(filename, rows_info, sheet='Result'):
if filename and sheet:
wb = wbook()
_sheet = wb.active
_sheet.title = sheet
row = _sheet.max_row
for line in rows_info:
if isinstance(line, str):
row_list = [line]
elif isinstance(line, dict):
row_list = list(line.values())
else:
try:
row_list = list(line)
except:
row_list = []
for col in range(0, len(row_list)):
col_info = row_list[col]
_sheet.cell(row, col + 1, col_info)
row += 1
wb.save(filename)
else:
return '文件和sheet不能为空'

来源:https://blog.csdn.net/weixin_42336574/article/details/89384441

标签:python,excel,写入,速度
0
投稿

猜你喜欢

  • 使用python爬虫获取黄金价格的核心代码

    2023-11-03 22:55:28
  • 什么是Ajax及Ajax的优势

    2007-09-07 09:56:00
  • 解决pycharm19.3.3安装pyqt5找不到designer.exe和pyuic.exe的问题

    2022-06-08 02:29:26
  • MySQL最好的优化技巧

    2009-10-27 12:05:00
  • Python爬取商家联系电话以及各种数据的方法

    2023-07-24 18:39:38
  • 微软建议的ASP性能优化28条守则(2)

    2008-02-22 17:02:00
  • Python3使用pandas模块读写excel操作示例

    2021-06-30 16:34:47
  • Python 使用 environs 库定义环境变量的方法

    2022-06-06 08:58:51
  • ASP 代码出现80040e14错误的解决方法

    2011-03-29 10:53:00
  • 举例讲解Python中metaclass元类的创建与使用

    2023-12-11 23:06:57
  • 如何增强网站数据库Access文件的安全性

    2008-11-13 16:58:00
  • Python3 pandas.concat的用法说明

    2023-11-22 23:46:51
  • Python实现自动签到脚本功能

    2022-07-24 21:53:40
  • PyQt5每天必学之拖放事件

    2021-02-28 19:26:15
  • Python 自动化修改word的案例

    2021-11-08 21:18:16
  • 从Web查询数据库之PHP与MySQL篇

    2023-07-18 11:04:05
  • 2020版Python学习路线图(附学习资料)

    2021-01-11 09:33:52
  • Python-split()函数实例用法讲解

    2023-12-12 07:13:20
  • Python使用Excel将数据写入多个sheet

    2022-01-20 11:52:08
  • javascript new fun的执行过程

    2010-08-05 21:23:00
  • asp之家 网络编程 m.aspxhome.com