| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677 |
- import os, json
- #操作word的库
- from docx import Document
- #设置尺寸的
- from docx.shared import Inches
- from lib.documentoper import DocumentOper
- class WordOper(DocumentOper):
-
- def __init__(self, fileName='', filePath='', fileType=''):
- super().__init__(fileName=fileName, filePath=filePath, fileType=fileType)
-
- def setXX(self):
- pass
-
- def export(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']
-
- # 文档保存
- document.save(super().setFullPath())
- return 'success'
- except Exception as ex:
- print(ex)
- return 'failure'
|