代码拉取完成,页面将自动刷新
# pip install python-docx
import yaml
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx import Document
from docx.shared import Pt
import os
from docx.oxml.ns import qn
if __name__ == '__main__':
doc_path = "./test.docx"
if os.path.exists(doc_path):
os.remove(doc_path)
with open("./static/data/space_padding_data.yaml", 'r', encoding='utf-8') as f:
dic = yaml.load(f.read(), Loader=yaml.FullLoader)
document = Document()
paragraph_title = document.add_paragraph()
run = paragraph_title.add_run('空间推演状态分析报告')
paragraph_title.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER # 居中
run.font.bold = True # 加粗
run.font.name = '宋体'
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run.font.size = Pt(15)
paragraph_center_title = document.add_paragraph()
run = paragraph_center_title.add_run("中心数据:")
run.font.name = '宋体'
run.font.bold = True # 加粗
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run.font.size = Pt(10)
center = " 传感器类别:" + str(dic['center']['describe']) + \
'\n 传感器编号:' + str(dic['center']['id']) + \
'\n 传感器名称:' + str(dic['center']['name']) + \
'\n 传感器坐标:(' + str(dic['center']['lng']) + ',' + str(dic['center']['lat']) + ')' + \
'\n 测量时间:' + str(dic['date']) + \
'\n 数值:' + str(dic['center']['value']) + '\n'
paragraph_center = document.add_paragraph()
run = paragraph_center.add_run(center)
run.font.name = '宋体'
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run.font.size = Pt(10)
# padding
paragraph_padding_title = document.add_paragraph()
run = paragraph_padding_title.add_run("推演数据:")
run.font.name = '宋体'
run.font.bold = True # 加粗
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run.font.size = Pt(10)
center = ""
for item in dic['padding']:
center += ' 推演点编号:' + str(item['id']) + \
'\n 推演点坐标:(' + str(item['lng']) + ',' + str(item['lat']) + ')' + \
'\n 数值:' + str(item['value']) + '\n\n'
paragraph_center = document.add_paragraph()
run = paragraph_center.add_run(center)
run.font.name = '宋体'
run._element.rPr.rFonts.set(qn('w:eastAsia'), u'宋体')
run.font.size = Pt(10)
# # 设置word字体大小
# style = document.styles['Normal']
# font = style.font
# font.size = Pt(10)
document.save('./test.docx')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。