# encode=utf-8
import numpy as np
import matplotlib.pyplot as plt
from docx import Document
from docx.shared import Inches
from wordcloud import WordCloud def func(pct, allvals):
absolute = int(np.round(pct / 100. * np.sum(allvals)))
return f"{pct:.1f}%\n({absolute:d})" # class Chart:
# def __init__(self, title, xlabel, ylabel):
# self.title = title
# self.xlabel = xlabel
# self.ylabel = ylabel
# self.data = []
#
# def add_data(self, x, y):
# self.data.append((x, y))
#
# def plot(self):
# fig, ax = plt.subplots()
# for x, y in self.data:
# ax.plot(x, y)
# plt.title(self.title)
# plt.xlabel(self.xlabel)
# plt.ylabel(self.ylabel)
# plt.tight_layout()
# plt.savefig('chart.png')
#
#
# class DocumentWithCharts:
# def __init__(self):
# self.document = Document()
# '''
# 添加封面图
# '''
# def add_pic(self, image_path):
# self.document.add_picture(image_path, width=Inches(5.0), height=Inches(3.0))
#
# def add_paragraph(self,text):
# self.document.add_paragraph(text)
#
# def add_heading(self,text):
# self.document.add_heading(text)
#
# def add_table(self,rows,cols):
# self.document.add_table(rows,cols)
#
# def add_chart(self, chart):
# self.document.add_heading(chart.title, level=0)
# # self.document.add_paragraph('This is a chart generated using Matplotlib.')
# chart.plot()
# self.document.add_picture('chart.png', width=Inches(6))
#
# def add_page_break(self):
# self.document.add_page_break()
#
# def save(self, filename):
# self.document.save(filename) document = Document()
# 添加封面图
image_path ='./xt.jpg'
document.add_picture(image_path, width=Inches(5.0), height=Inches(3.0))
# 添加大标题
document.add_heading('无敌潇洒测试')
# 添加空段落
document.add_paragraph()
# 添加有序段落
document.add_paragraph('信息综述',style='List Number')
# 添加综述
fx_content = '本周期内,共监测到关于 正负面报告 的互联网信息数量为 4819 篇,监测到正面信息 331 篇,占比 6.87% ,中性信息 3527 篇,占比 73.19% ,负面信息 961 篇,占比 19.94%。'
document.add_paragraph(fx_content)
# 添加饼图
fig, ax = plt.subplots(figsize=(6, 3), subplot_kw=dict(aspect="equal")) recipe = ["60 negativity",
"75 neutral",
"250 positive"
]
data = [float(x.split()[0]) for x in recipe]
ingredients = [x.split()[-1] for x in recipe] colors = ['red', 'gray', 'green']
wedges, texts, autotexts = ax.pie(data,
autopct=lambda pct: func(pct, data), #自动添加百分比显示
textprops=dict(color="w"), # 设置标签里的字体大小,颜色属性
colors=colors #饼状填充的颜色
)
ax.legend(wedges, ingredients,
title="Ingredients",
loc="center left",
bbox_to_anchor=(1, 0, 0.5, 1))
plt.setp(autotexts, size=8, weight="bold")
ax.set_title("qxx")
plt.savefig('./qxx.jpg')
document.add_picture('./qxx.jpg')
# 增加分页
document.add_page_break()
# 表格数据
records = ( (90, 80, 70), )
table = document.add_table(rows=1,cols=3) #设置表头
hdr_cells = table.rows[0].cells
hdr_cells[0].text = 'negativity'
hdr_cells[1].text = 'neutral'
hdr_cells[2].text = 'positive'
for a,b,c in records:
#为表格添加一行
row_cells = table.add_row().cells
row_cells[0].text = str(a)
row_cells[1].text = str(b)
row_cells[2].text = str(c) # 添加空行
document.add_paragraph() # 添加折线图
document.add_paragraph('传播趋势',style='List Number')
fig1,ax1 = plt.subplots()
ax1.plot([1, 2, 3, 4], [1, 4, 9, 16])
plt.savefig('./d1.jpg')
document.add_picture('./d1.jpg') # 添加空行
document.add_paragraph('词云图',style='List Number')
document.add_paragraph()
# 定义文本数据,包含中文和英文单词
text = "Python is a popular programming language. It is widely used for data analysis, machine learning, and web development. Python是一种流行的编程语言。它被广泛应用于数据分析、机器学习和Web开发。" # 创建WordCloud对象,设置背景颜色、字体最小尺寸等参数
wordcloud = WordCloud(font_path='./simhei.ttf', # 设置中文字体路径,这里使用黑体字体
background_color='white', # 设置背景颜色为白色
min_font_size=10, # 设置字体最小尺寸
width=200, # 设置词云图表宽度
height=100, # 设置词云图表高度
max_words=50, # 设置最大单词数
max_font_size=30) # 设置最大字体尺寸 # 使用WordCloud对象生成词云图表
wordcloud.generate(text) # 使用matplotlib显示词云图表
plt.figure(figsize=(10, 10)) # 设置matplotlib图表大小
plt.imshow(wordcloud, interpolation='bilinear') # 显示词云图表,使用双线性插值提高图像质量
plt.axis("off") # 关闭坐标轴显示 plt.savefig('./cy.jpg') document.add_picture('./cy.jpg')
document.save('my_document.docx')

python WordCloud matplotlib docx 简易生成docx报告的更多相关文章

  1. python笔记32-ddt框架优化(生成html报告注释内容传变量)

    前言 至于什么是ddt这个可以参考我之前写的博客内容,使用ddt框架的时候,有个问题困扰我很久了,一直没得到解决(也有很大小伙伴问过我,没解决抱歉了!) 这个问题就是:如何使用ddt框架时,生成的ht ...

  2. pytest系列(四)- pytest+allure+jenkins - 持续集成平台生成allure报告

    pytest是什么 pytest是python的一款测试框架,拥有unittest的功能并比它更丰富. allure是什么 有非常多的优秀的测试框架,但却是有非常少优秀的报告工具可以展示非常清楚的用例 ...

  3. 利用Python半自动化生成Nessus报告

    0x01 前言 Nessus是一个功能强大而又易于使用的远程安全扫描器,Nessus对个人用户是免费的,只需要在官方网站上填邮箱,立马就能收到注册号了,对应商业用户是收费的.当然,个人用户是有16个I ...

  4. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告

    1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请求参数 ...

  5. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(二)

    可以参考 python+requests接口自动化完整项目设计源码(一)https://www.cnblogs.com/111testing/p/9612671.html 原文地址https://ww ...

  6. Python不生成HTMLTestRunner报告-转载学习

    1.问题:Python中同一个.py文件中同时用unittest框架和HtmlReport框架后,HtmlReport不被执行. 2.为什么?其实不是HtmlReport不被执行,也不是HtmlRep ...

  7. 基于Mybatis的Mysql数据库文档生成工具,支持生成docx(原创)

    今天不写android--也写写数据库相关的东西 -------------------- 今日老夫闲来无事,设计了一款数据库文档生成工具 眼下仅仅支持mysql 主要是生成docx的 下载链接:下载 ...

  8. python+requests+excel+unittest+ddt接口自动化数据驱动并生成html报告(已弃用)

    前言 1.环境准备: python3.6 requests xlrd openpyxl HTMLTestRunner_api 2.目前实现的功能: 封装requests请求方法 在excel填写接口请 ...

  9. Python如何实现doc文件转换为docx文件?

    Python如何实现doc文件转换为docx文件? 在开发过程中遇到一个关于读写doc和docx的问题: 一个文件夹中有两种文件, 一种为doc结尾, 一种为docx结尾, 需要将这些文件全部重命名. ...

  10. python生成HTMl报告(unittest)

      Python3 使用HTMLTestRunner.py 报错ImportError: No module named 'StringIO'处理方法 HTMLTestRunner.py文件是基于Py ...

随机推荐

  1. Vue源码学习(十五):diff算法(二)交叉比对(双指针)

    好家伙, 本节来解决我们上一章留下来的问题, 新旧节点同时有儿子的情况本章继续解决 1.要做什么? 本章将解决, 1.在相同tag下子元素的替换问题 2.使用双指针进行元素替换, 实现效果如下:   ...

  2. base64编码的学习

    base64编码是什么 Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法. Base64编码本质上是一种将二进制数据转成 ...

  3. Modbus转Profinet 网关 TS-180

    产品简介 实现 PROFINET 网络与串口网络之间的数据通信,三个串口可分别连接具有 RS232 或 RS485 接口的设 备到 PROFINET 网络.即将串口设备转换为 PROFINET 设备. ...

  4. TS版LangChain实战:基于文档的增强检索(RAG)

    LangChain LangChain是一个以 LLM (大语言模型)模型为核心的开发框架,LangChain的主要特性: 可以连接多种数据源,比如网页链接.本地PDF文件.向量数据库等 允许语言模型 ...

  5. 【Javaweb】implements Serializable是什么意思?反序列化是什么意思?

    为了保证数据传输的可靠 性,常常要implements Serializable,为什么? 对象本质上是虚无缥缈的,只是内存中的一个地址,如果想要让对象持久化,让对象在网络上传输,总不可能传送一个内存 ...

  6. 【matlab混沌理论】1.2.洛伦兹吸引子

    ​Lorenz洛伦兹吸引子定义洛伦兹函数组后,通过ode45函数求解此微分方程方程. input: % Lorenz函数的洛伦兹吸引子 % 2.定义模型参数 sigma = 10; beta = 8/ ...

  7. 13 HTTP传输大文件的方法

    目录 如何在有限的带宽下高效快捷传输大文件? 数据压缩 分块传输 范围请求 多段数据 如何在有限的带宽下高效快捷传输大文件? 数据压缩 分块传输 范围请求 多段数据 数据压缩 思路:把大文件整体变小 ...

  8. Cocos内存管理解析 CCRef/retain/release/autorelease

    Cocos内存管理源码(autorelease解析) 背景 这段时间在做项目的时候,需求需要往spine动作的挂点上绑定按钮节点,由于按钮在编辑器中是加在已有节点上的,所以在往spine上添加挂点时, ...

  9. lxml模块

    lxml主要用xpath.css选择器等来提取xml格式文档,html也是xml格式文档的一种. xpath方法返回列表的三种情况 返回空列表:没有找到任何元素 返回字符串列表:xpath规则匹配用了 ...

  10. 【笔记整理】request模块基本使用

    基本使用 发送get请求.获取响应各种请求.响应信息 def fun1(): url = "http://www.baidu.com" resp = requests.get(ur ...