3 Star 0 Fork 0

李泊昊/background-shanghai

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
test_doc.py 2.63 KB
一键复制 编辑 原始数据 按行查看 历史
李泊昊 提交于 2022-05-18 20:03 . 空间推演
# 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')
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/LIPO714/background-shanghai.git
git@gitee.com:LIPO714/background-shanghai.git
LIPO714
background-shanghai
background-shanghai
master

搜索帮助