专注度pdf报告生成代码编写:
在大模型处理完毕后需要生成针对课程的个人专注度pdf和班级专注度的pdf结果,这个结果由以下代码进行生成,主要为生成评估报告和分析图表。以下为代码:
"""
@Time : 2024/6/22 11:06
@Auth : huxinyu
@File :generate_pdf.py
@IDE :PyCharm
"""
import matplotlib.pyplot as plt
from reportlab.lib.pagesizes import letter
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.utils import ImageReader
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
import io
from PIL import Image
# 用来正常显示中文标签
plt.rcParams['font.sans-serif'] = ['STSong']
pdfmetrics.registerFont(TTFont("SimSun", "SimSun.ttf"))
# 创建 PDF 文件
def create_pdf1(filename):
c = canvas.Canvas(filename, pagesize=letter)
width, height = letter
# 添加标题
title = "个人专注度分析报告"
c.setFont("SimSun", 16)
c.drawCentredString(width / 2.0, height - 1 * inch, title)
# 添加文本
c.setFont("SimSun", 12)
c.drawString(1 * inch, height - 1.5 * inch, "该同学观看当前视频的平均专注度为{}%,评级为{};\n在第{}分钟内专注度不足70%,疑似走神;".format(value,grade,min))
# 添加图表
create_chart1(c, width, height)
# 保存 PDF
c.save()
# 创建图表并添加到 PDF
def create_chart1(c, width, y_position):
# 示例数据
minutes = list(range(1, 12)) # 1 到 11 分钟
percentages = [91.6, 90.2, 84.1, 80.9, 75.8, 72.6, 74.6, 75.9, 75.1, 78.5, 78.1] # 每分钟对应的百分比
# 创建 Matplotlib 图表
fig, ax = plt.subplots()
ax.plot(minutes, percentages, marker='o', linestyle='-', color='b')
ax.set_xlabel('分钟')
ax.set_ylabel('专注度 (%)')
ax.set_title('专注度随时间变化')
ax.set_xticks(minutes)
ax.set_yticks(range(0, 101, 10))
ax.grid(True)
# 将图表保存到内存中的字节流
img_buffer = io.BytesIO()
plt.savefig(img_buffer, format='PNG')
img_buffer.seek(0)
# 使用 PIL Image 打开图像并保存为 BytesIO 对象
image = Image.open(img_buffer)
img_buffer.seek(0)
image.save(img_buffer, format='PNG')
# 使用 ImageReader 读取 BytesIO 对象
img = ImageReader(img_buffer)
# 将图表添加到 PDF
c.drawImage(img, 1 * inch, y_position - 6 * inch, width=6 * inch, height=4 * inch)
# 关闭图表以释放内存
plt.close(fig)
# 创建 PDF 文件
def create_pdf2(filename):
c = canvas.Canvas(filename, pagesize=letter)
width, height = letter
# 添加标题
title = "班级专注度分析报告"
c.setFont("SimSun", 16)
c.drawCentredString(width / 2.0, height - 1 * inch, title)
# 添加文本
c.setFont("SimSun", 12)
c.drawString(1 * inch, height - 1.5 * inch, "该班级同学观看当前视频的平均专注度为{}%,评级为{};".format(avg,grad))
# 添加图表
create_chart2(c, width, height)
# 保存 PDF
c.save()
# 创建图表并添加到 PDF
def create_chart2(c, width, y_position):
# 示例数据
student_ids = list(range(1, 2)) # 学生编号
percentages = [80.9] # 对应的专注度百分比
# 创建 Matplotlib 条形图
fig, ax = plt.subplots()
ax.bar(student_ids, percentages, color='cornflowerblue',width=0.2, align='center')
ax.set_xlabel('学生编号')
ax.set_ylabel('专注度 (%)')
ax.set_title('学生专注度')
ax.set_xticks(student_ids)
ax.set_yticks(range(0, 101, 10))
ax.set_xlim(0.5, 1.5) # 设置 x 轴范围,确保柱子不会太宽
ax.grid(True, axis='y')
# 将图表保存到内存中的字节流
img_buffer = io.BytesIO()
plt.savefig(img_buffer, format='PNG')
img_buffer.seek(0)
# 使用 PIL Image 打开图像并保存为 BytesIO 对象
image = Image.open(img_buffer)
img_buffer.seek(0)
image.save(img_buffer, format='PNG')
# 使用 ImageReader 读取 BytesIO 对象
img = ImageReader(img_buffer)
# 将图表添加到 PDF
c.drawImage(img, 1 * inch, y_position - 6 * inch, width=6 * inch, height=4 * inch)
# 关闭图表以释放内存
plt.close(fig)
# 运行示例
if __name__ == "__main__":
# create_pdf1("test.pdf")
create_pdf2("test2.pdf")