数据分析报告

该文档使用Python的docx和openpyxl库自动生成了一份期末数据分析报告,包括标题设置、表格创建、数据读取(Excel文件中的学生信息表)、成绩统计(如不及格人数、平均分、最高分)、等级划分以及原因分析。
摘要由CSDN通过智能技术生成
"""
Author:Hook
Create Time:2023-02-18 9:39
身体健康,日富一日!!!
我与我周旋久 宁做我.
"""

from docx import Document
import openpyxl
from docx.enum.style import WD_STYLE_TYPE,WD_BUILTIN_STYLE
from docx.shared import Pt, Cm, RGBColor
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from docx.oxml.ns import qn
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT

# 创建一个空的Word文档
doc = Document()

# 通过run给文档添加标题
h2 = doc.add_heading('2022年下半学期',level=2)
h1 = doc.add_heading(level=1)           # 创建一个空标题对象
run1 = h1.add_run('期末数据分析报告')
run2 = h1.style.font.bold=True
run3 = h1.style.font.size = Pt(70)
run4 = h1.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
h1.style.font.color.rgb = RGBColor(105, 105, 105)
run5 = h1.paragraph_format.space_before = Pt(20)
run6 = h2.paragraph_format.space_before = Pt(20)

h3 = doc.add_heading('以下为学生信息表:', level=1)
run7 = h3.style.font.size = Pt(15)

wb = openpyxl.open('files/学生信息.xlsx')
sheet = wb['students']
m_r = sheet.max_row
m_c = sheet.max_column
# 本班总人数  最大行-标题行
print('本班人数为: ', m_r-1)
# 创建表
table1 = doc.add_table(m_r+1, m_c, style='Medium Shading 1 Accent 1')
table1.style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
table1.add_column(Pt(100))
# 获取整个表所有内容
result = []
for row in range(2, m_r+1):
    line = {}
    for col in range(1, m_c+1):
        key = sheet.cell(1, col).value
        value = sheet.cell(row, col).value
        line.setdefault(key, value)
    result.append(line)

 # 添加一列存放学生等级
table1.cell(1,4).text = '等级'

# 拿到每列需要填充的数据
for row in range(1,m_r+1):
    cell1 = sheet.cell(row, 1)
    cell2 = sheet.cell(row, 2)
    cell3 = sheet.cell(row, 3)
    cell4 = sheet.cell(row, 4)
    table1.cell(row, 0).text = cell1.value
    table1.cell(row, 1).text = str(cell2.value)
    table1.cell(row, 2).text = cell3.value
    table1.cell(row, 3).text = str(cell4.value)


p1 = doc.add_paragraph()
run1 = p1.add_run('下面关于学生成绩做出相关分析结论:')
run1.font.bold =True
run1.font.color.rgb =RGBColor(135,206,250)
p2 = doc.add_paragraph()
run2 = p2.add_run('已知各科成绩满分均为100分,关于等级的评定:')
run3 = p2.add_run('100分~90分为A级、89分~75分为B级、74分~60分为C级、59分及其以下为D级')

p11 = doc.add_paragraph()
run_sum = p11.add_run('学生总人数为:')
run_sum = p11.add_run('48')
run_sum.font.color.rgb = RGBColor(255, 0, 0)


# 统计不及格人数
count = 0
for i in range(2, m_r+1):
    scores = int(sheet.cell(i,4).value)
    if scores < 60:
        count += 1
print('本班不及格的人数为: ',count)
p3 = doc.add_paragraph()
run8 = p3.add_run('1、不及格学生的人数为:')
run9 = p3.add_run('21')
run9.font.color.rgb = RGBColor(255, 0, 0)

# 平均分
scores_sum = 0
for x in range(2, m_r+1):
    scores = int(sheet.cell(x,4).value)
    scores_sum += scores
    avg_score = round(scores_sum/x, 2)

print('本班平均分为: ', avg_score)
p4 = doc.add_paragraph()
run10 = p4.add_run('2、本班的平均分为:')
run10 = p4.add_run('66.57')
run10.font.color.rgb = RGBColor(255, 0, 0)

# 本班最高分
max_scores = 0
for i in range(2,m_r+1):
    max1 = int(sheet.cell(i, 4).value)
    if max1 > max_scores:
        max_scores = max1
    else:
        max_scores = max_scores
print('最高分为:', max_scores)
p5 = doc.add_paragraph()
run11 = p5.add_run('3、本班的最高分为:')
run11 = p5.add_run('99')
run11.font.color.rgb = RGBColor(255, 0, 0)



# 统计学生分数的等级 并求出每个等级学生数量
countA = 0
countB = 0
countC = 0
countD = 0
for i in range(2, m_r+1):
    scores = int(sheet.cell(i,4).value)
    if scores > 90:
        table1.cell(i, 4).text = 'A'
        countA += 1
    elif scores > 75:
        table1.cell(i, 4).text = 'B'
        countB += 1
    elif scores > 60:
        table1.cell(i, 4).text = 'C'
        countC += 1
    else:
        table1.cell(i, 4).text = 'D'
        countD += 1
print('等级为A的学生有:', countA, '人')
print('等级为B的学生有:', countB, '人')
print('等级为C的学生有:', countC, '人')
print('等级为D的学生有:', countD, '人')

p5 = doc.add_paragraph()
runA = p5.add_run('4、等级为A的学生为:')
runA = p5.add_run('8')
runA.font.color.rgb = RGBColor(255, 0, 0)

p6 = doc.add_paragraph()
runB = p6.add_run('等级为B的学生为:')
runB = p6.add_run('9')
runB.font.color.rgb = RGBColor(255, 0, 0)

p7 = doc.add_paragraph()
runC = p7.add_run('等级为C的学生为:')
runC = p7.add_run('9')
runC.font.color.rgb = RGBColor(255, 0, 0)

p8 = doc.add_paragraph()
runD = p8.add_run('等级为B的学生为:')
runD = p8.add_run('22')
runD.font.color.rgb = RGBColor(255, 0, 0)

# 产生这个分数的原因分析
h4 = doc.add_heading('分数原因分析:')
p9 = doc.add_paragraph('本次期末考试的成绩并不理想,从总体上来看,试卷稍难,有点高考题的难度走向,对中等和中等偏下的学生来说,考的十分不理想, 由此说明学生不能只死记硬背,要多思考,灵活应用知识,再有就是不放过任何课本上出现的题,这次试卷有好几题都是课后练习题。')
# 段前间距
p9.paragraph_format.space_before = Pt(20)
# 行间距
p9.paragraph_format.line_spacing = 1.5
# 首行缩进
p9.paragraph_format.first_line_indent = Pt(40)
doc.save('files/期末数据分析报告.docx')





``

运行结果

请添加图片描述

效果图

请添加图片描述请添加图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值