当前位置:   article > 正文

python 使用 openpyxl 操作 excel_openpyxl.get

openpyxl.get

python 使用 openpyxl 操作 excel


前言

二、使用 zipfile、openpyxl、flask 批量导出excel zip


1.环境

openyxl:3.0.6

python:3.7.6

pandas:1.3.5

2.读取数据

#使用pandas读取数据
#https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html#pandas.read_excel
pd.read_excel(path, sheet_name=None)
  • 1
  • 2
  • 3

#flask form
pd.read_excel(request.files['file'], sheet_name=None)
  • 1
  • 2

具体操作

https://pandas.pydata.org/docs/reference/frame.html
  • 1

3.openpyxl

加载excel

#加载excel
#https://pandas.pydata.org/docs/reference/api/pandas.read_excel.html#pandas.read_excel
wb = load_workbook('./src/statics/clean_production_tmp.xlsx')
  • 1
  • 2
  • 3

创建excel

wb = Workbook()
  • 1
e方法参数作用返回
Workbookgetwb.active获取当前的sheet页WorkSheet
Workbookgetwb.get_sheet_by_name()str根据sheet名称获取sheet页WorkSheet
Workbookgetwb.copy_worksheet()worksheet根据参数 worksheet 复制worksheetWorkSheet
Workbooksetwb.remove(worksheet)worksheet根据参数 worksheet 复制worksheetWorkSheet
WorkSheetsetws.column_dimensions[‘A’].width = 10设置列宽
WorkSheetsetws.title = ‘123’str设置sheet标题
WorkSheetsetws.merge_cells(“A1:A4”)set合并单元格

保存excel

wb = Workbook()
wb.save('balances.xlsx')
  • 1
  • 2

通过接口返回

output = BytesIO()
wb_tmp = load_workbook('./src/statics/clean_production_tmp.xlsx')
wb_tmp.save(output)
output.seek(0)
resp = make_response(output.getvalue())
basename = '2022-3-7.xlsx'

# 转码,支持中文名称
resp.headers["Content-Disposition"] = "attachment; filename*=UTF-8''{utf_filename}".format(utf_filename=basename)
resp.headers['Content-Type'] = 'application/x-xlsx'
return resp
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/834416
推荐阅读
相关标签
  

闽ICP备14008679号