| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124 |
- import os, json
- from flask import make_response, send_from_directory
- from openpyxl import Workbook
- from docx import Document
- from docx.shared import Inches
- class FileOper():
- def download(self, filename): #下载的方法
- directory = os.getcwd() #获取当前程序的工作路径
-
- response = make_response(send_from_directory( directory+'/upload/xlsx/',
- filename, as_attachment=True))
- #设置头,表示这是一个附件
- response.headers["Content-Disposition"] = "attachment; filename={}".format(filename)
- return response
-
- def downloadword(self, filename): #下载的方法
- directory = os.getcwd() #获取当前程序的工作路径
-
- response = make_response(send_from_directory( directory+'/upload/docx/',
- filename, as_attachment=True))
- #设置头,表示这是一个附件
- response.headers["Content-Disposition"] = "attachment; filename={}".format(filename)
- return response
-
- #导出Excel文档的方法
- def exportExcel(self, exportData):
- try:
- #要生成的数据
- exportObj = json.loads(exportData)
-
- #创建一个工作簿
- wb = Workbook()
- #创建工作表
- wb.create_sheet(title="全国", index =0 )
- #设置为当前的工作区
- ws = wb.active
-
- #设置单元格
- ws['A1']= '序号'
- ws['B1'] = '疫情地区'
- ws['C1'] = '新增'
- ws['D1'] = '现有'
- ws['E1'] = '累计'
- ws['F1'] = '治愈'
- ws['G1'] = '死亡'
-
- for index, item in enumerate(exportObj['list']):
- ws.append([index+1, item['area'],item['confirmedRelative'],
- item['curConfirm'],item['confirmed'], item['crued'], item['died']])
- #保存
- wb.save("upload/xlsx/global_1902_kxy.xlsx")
-
- return 'success'
- except Exception as e:
- print(e)
- #pass
- return 'failure'
-
- #生成Word方法
- def exportWord(self, exportData):
- try:
- #要生成的数据
- exportObj = json.loads(exportData)
-
- #首先创建一个Word文档对象
- document = Document()
- #添加标题
- document.add_heading('新型冠状病毒肺炎疫情',0)
-
- # 添加段落
- p = document.add_paragraph('全国 实时更新_1902_kxy')
- p.add_run('粗体').bold = True
- p.add_run(' 普通文本 ')
- p.add_run('斜体.').italic = True
-
- # 添加1级标题
- document.add_heading('一级标题样式', level=1)
- # 添加段落,同时设置样式
- document.add_paragraph('缩进引用样式', style='Intense Quote')
- document.add_paragraph(
- '无序列表样式', style='List Bullet'
- )
- document.add_paragraph(
- '有序列表样式', style='List Number'
- )
-
- # 添加图片 ,同时设置大小
- document.add_picture('upload/img/test_1902_kxy.png', width=Inches(3.25))
-
- # 添加分页符
- document.add_page_break()
-
- # 添加表格
- table = document.add_table(rows=1, cols=7)
- hdr_cells = table.rows[0].cells
- hdr_cells[0].text = '序号'
- hdr_cells[1].text = '疫情地区'
- hdr_cells[2].text = '新增'
- hdr_cells[3].text = '现有'
- hdr_cells[4].text = '累计'
- hdr_cells[5].text = '治愈'
- hdr_cells[6].text = '死亡'
-
- for index, item in enumerate(exportObj['list']):
- row_cells = table.add_row().cells
- row_cells[0].text = str(index+1)
- row_cells[1].text = item['area']
- row_cells[2].text = item['confirmedRelative']
- row_cells[3].text = item['curConfirm']
- row_cells[4].text = item['confirmed']
- row_cells[5].text = item['crued']
- row_cells[6].text = item['died']
-
- # 将文档保存到demo.docx中
- document.save("upload/docx/global_1902_kxy.docx")
- return 'success'
-
- except Exception as ex:
- print(ex)
- return 'failure'
|