当前位置:网站首页>使用 zipfile、openpyxl、flask 批量导出excel zip
使用 zipfile、openpyxl、flask 批量导出excel zip
2022-06-22 14:36:00 【mtl1994】
使用 zipfile、openpyxl、flask 批量导出excel zip
文章目录
前言
一、python 使用 openpyxl 操作 excel https://blog.csdn.net/mtl1994/article/details/123349397
一、环境
- python3.10 (https://docs.python.org/3/library/zipfile.html)
二、使用
1.压缩本地文件
with ZipFile('spam.zip', 'w') as myzip:
myzip.write('1.xls')
myzip.write('app.py')
2.压缩流文件到zip
xls_byte = io.BytesIO()
xls_tmp = load_workbook('1.xlsx')
xls_tmp.save(xls_byte)
with ZipFile('spam.zip', 'w') as myzip:
myzip.writestr('1.xlsx', xls_byte.getvalue())
xls_byte.seek(0)
3.压缩目录下 pyc
project = PyZipFile("project.zip", mode='w')
project.writepy("d:/datasets-work")
4.openpyxl 生成多个excel, excel zipfile 压缩 flask接口 导出
@admin.route("/export_all_tmplate", methods=["GET"])
def export_all_tmplate():
all_zip = BytesIO()
zf = ZipFile(all_zip, 'w')
for i in range(10):
output = BytesIO()
wb_tmp = load_workbook(f'tmp-{i}.xlsx')
wb_tmp.save(output)
"""
操作excel...
"""
zf.writestr(f"tmp-{i}.xlsx", output.getvalue())
output.seek(0)
zf.close()
all_zip.seek(0)
resp = make_response(all_zip.getvalue())
basename = 'all.zip'
# 转码,支持中文名称
resp.headers["Content-Disposition"] = "attachment; filename*=UTF-8''{utf_filename}".format(
utf_filename=basename)
resp.headers['Content-Type'] = 'application/zip'
return resp
5.实例
@admin.route("/equipment_standing_book/basic/export_all_tmplate", methods=["GET"])
# @need_login
# @cache.cached(timeout=600, key_prefix=make_cache_key)
def equipment_standing_book_export_all_tmplate():
"""
#group 导出接口
#name 导出接口
#desc 导出excel
#param tmp_name #模板名称
#priv need_login
#return data <dict> 各个字段的注释
#example
{
"code": 0,
"msg": "成功",
"data":{}
}
"""
tmp_name = str(g.request_data.get("tmp_name", "")).strip()
time = str(g.request_data.get("time", "")).strip()
filename = time
tmplate = equip_standing_config.query({'tmp_name': tmp_name})[0]
format = "%Y"
if tmplate['type'] == '3':
format = "%Y-%m"
all_zip = BytesIO()
zf = ZipFile(all_zip, 'w')
record_list = equip_standing_record.query(where=f" and config_id='{tmplate['id']}' and date_format(create_time, '{format}') = '{time}'")
last_record_time = ""
for ri, record in enumerate(record_list):
output = BytesIO()
#如果相同日期合并到一个excel,多sheet
if last_record_time != "" and last_record_time == record["create_time"].strftime('%Y-%m-%d'):
ws = wb_tmp["next"]
ws.title = record['code'].replace(":", "")
target = wb_tmp.copy_worksheet(wb_tmp.active)
target.title = "next"
else:
wb_tmp = load_workbook(f'./src/statics/{tmp_name}.xlsx')
ws = wb_tmp[wb_tmp.sheetnames[0]]
ws.title = record['code'].replace(":", "")
target = wb_tmp.copy_worksheet(wb_tmp.active)
target.title = "next"
detail_list = eval(tmp_name).query({'record_id': record['id']})
obj: {} = eval(tmp_name).get_dict()
start, start_row, points = 0, [], {}
#找到从哪一行赋值 excel中 配置和实体类对应的字段
for i, row in enumerate(ws.values):
print(type(row))
start_row = row
for j, value in enumerate(row):
print(i,j,value)
if value in obj.keys():
points[j] = value
start = i
if value == 'serial_number':
points[j] = 'serial_number'
start = i
if start > 0:
break
ws.delete_rows(start + 1)
ws.insert_rows(start + 1, len(detail_list))
#赋值
for i, obj in enumerate(detail_list):
for point in points.keys():
cell = ws.cell(row=start + 1, column=point + 1)
cell.value = i if points[point] == 'serial_number' else obj[points[point]]
cell.border = Border(top=Side(border_style='thin', color='000000'),
bottom=Side(border_style='thin', color='000000'),
left=Side(border_style='thin', color='000000'),
right=Side(border_style='thin', color='000000'))
start += 1
#如果当前行和上一行相同,合并
for i in range(start + 1, ws.max_row + 1):
ws.merge_cells(start_row=i, start_column=1, end_row=i, end_column=ws.max_column)
if (ri + 1 < len(record_list) and record_list[ri + 1]["create_time"].strftime('%Y-%m-%d') != record_list[ri]["create_time"].strftime('%Y-%m-%d'))\
or (last_record_time != "" and last_record_time != record["create_time"].strftime('%Y-%m-%d')):
wb_tmp.remove(wb_tmp["next"])
wb_tmp.save(output)
zf.writestr(record['create_time'].strftime('%Y-%m-%d') + ".xlsx", output.getvalue())
output.seek(0)
last_record_time = record["create_time"].strftime('%Y-%m-%d')
zf.close()
all_zip.seek(0)
resp = make_response(all_zip.getvalue())
basename = f'{filename}.zip'
# 转码,支持中文名称
resp.headers["Content-Disposition"] = "attachment; filename*=UTF-8''{utf_filename}".format(
utf_filename=basename)
resp.headers['Content-Type'] = 'application/zip'
return resp
总结
~~~
边栏推荐
猜你喜欢

What happened to those who didn't go to college

Hongshi electric appliance rushes to the Growth Enterprise Market: the annual revenue is 600million yuan. Liujinxian's equity was frozen by Guangde small loan

FreeRTOS task priority and interrupt priority

Discourse 的信任级别

二分查找(整数二分)

极致效率,云原生数据库TDSQL-C安身立命的根本

润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东

TDengine 连接器上线 Google Data Studio 应用商店

【newman】postman生成漂亮的测试报告
![[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want](/img/cb/826b3591bafa62cc71826732eb32ef.png)
[single chip microcomputer] [make buzzer sound] know the buzzer and let it make the sound you want
随机推荐
大佬们 2.2.1cdc 监控sqlsever 只能拿到全量的数据 后期增量的数据拿不到 咋回事啊
Quick sort_ sort
C语言学习-17-函数作为参数传入函数
Merge sort of sorting
小白操作Win10扩充C盘(把D盘内存分给C盘)亲测多次有效
mysql如何将字段修改为not null
FPGA采集DHT11温湿度
Community article | mosn building subset optimization ideas sharing
[Newman] postman generates beautiful test reports
Rosbag使用命令
Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
The 12 SQL optimization schemes summarized by professional "brick moving" old drivers are very practical!
"Forget to learn again" shell process control - 38. Introduction to while loop and until loop
Promoting compatibility and adaptation, enabling coordinated development of gbase may adaptation Express
How MySQL modifies a field to not null
Ask if you want to get the start of sqlserver_ Is there a good way for LSN?
Scala language learning-04-function passed in as parameter function as return value
打新债安全性有多高
Fast and accurate point cloud registration based on minimizing 3D NDT distance
专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!