您好,登錄后才能下訂單哦!
本篇文章為大家展示了python中寫入excel的方法有哪些,內(nèi)容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細(xì)介紹希望你能有所收獲。
首先使用openpyxl進(jìn)行寫入操作,代碼如下:
book = openpyxl.Workbook() auths = Auth.objects.filter(owner_id=1) filename = '導(dǎo)出數(shù)據(jù)' for auth in auths: sheet = book.create_sheet(auth.name, index = 0) sheet.append([ _("書名"), _("作者"), _("譯者"), _("出版社"), _("序列號"), _("總頁數(shù)"), ]) 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寫入數(shù)據(jù):
book = xlwt.Workbook() auths = Auth.objects.filter(owner_id=1) filename = '導(dǎo)出數(shù)據(jù)' 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, _("總頁數(shù)")) 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寫入數(shù)據(jù):
book = xlsxwriter.Workbook(output) auths = Auth.objects.filter(owner_id=1) for auth in auths: sheet = book.add_worksheet(sensor.name) header = [ _("書名"), _("作者"), _("譯者"), _("出版社"), _("序列號"), _("總頁數(shù)"), ] 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)
三者的時間比較(兩種方式的文件內(nèi)容是一樣的):
openpyxl: 文件大小為110.75kb, 平均時間大約為570ms
xlwt: 文件大小為505.91kb,平均時間大約為440ms
XlsxWrite: 文件大小為109.28kb,平均時間大約為500ms
xlwt寫入的行數(shù)有限制,因此對于較大的文件來說,XlsxWrite的速度較快一點
補充知識:python寫入excel文件太慢如何解決-python往excel寫入大量數(shù)據(jù)
目前用的openpyxl,從數(shù)據(jù)庫獲取8W行的數(shù)據(jù)通過openpyxl寫入excel,要花費接近8分鐘,這也太慢了,用kettle的插件秒進(jìn),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不能為空'
上述內(nèi)容就是python中寫入excel的方法有哪些,你們學(xué)到知識或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識儲備,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。