day10-python操作word文档作业

from docx import Document
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      # 单元格垂直对齐方式
import openpyxl

doc = Document()
# 封面设计
h2 = doc.add_heading('万锋IT教育',level=2)
h2.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
h2.style.font.size = Pt(20)
h2.style.font.color.rgb = RGBColor(0,0,0)
h2.paragraph_format.space_after = Pt(150)

h1 = doc.add_heading('Python数据分析班成绩分析报告',level=1)
h1.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
h1.style.font.size = Pt(40)
h1.style.font.color.rgb = RGBColor(0,0,0)

h3 = doc.add_heading('单位:后勤组',level=3)
h3.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER
h3.style.font.size = Pt(20)
h3.style.font.color.rgb = RGBColor(0,0,0)
h3.paragraph_format.space_before = Pt(200)

# 正文部分
doc.add_page_break()
p1 = doc.add_paragraph('以下是python数据分析班的成绩表:')
p1.style.font.name = '宋体'
r = p1.style._element.rPr.rFonts
r.set(qn('w:eastAsia'),'宋体')
p1.style.font.size = Pt(12)

# 读excel文件
wb = openpyxl.open('files/学生信息 (1).xlsx')
# sheet = wb.active
sheet = wb['students']
m_r = sheet.max_row
m_c = sheet.max_column
table = doc.add_table(m_r, m_c, style='Medium Shading 2 Accent 5')

for row in range(1,m_r+1):
    for col in range(1,m_c+1):
        table.cell(row-1,col-1).text = str(sheet.cell(row,col).value)

table.style.font.size = Pt(12)
table.style.font.color.rgb = RGBColor(100,89,10)
table.style.paragraph_format.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER

# 总人数
stu_num = m_r-1
print(stu_num)
# 最高分
scores = []
for row in range(1,m_r):
    scores.append(int(table.cell(row,3).text))
max_score = max(scores)
max_score_count = 0
for x in scores:
    if x == max_score:
        max_score_count += 1
# print(max_score_count)

# 平均分
all_scores = 0
for x in scores:
    all_scores += x
mean = all_scores / stu_num
print(mean)

# 及格率
count = 0
for x in scores:
    if x >= 60:
        count += 1
pass_stu = count / stu_num
print(pass_stu)


p2 = doc.add_paragraph('这个班的总人数是:' + str(stu_num))
p2.paragraph_format.space_before = Pt(30)
doc.add_paragraph('这个班的最高分是:'+ str(max_score) + ',并且获得最高分的有:' + str(max_score_count) + '人')
doc.add_paragraph('这个班的平均分是:'+ str(mean))
doc.add_paragraph('这个班的及格率是:' + str(pass_stu))




doc.save('files/成绩分析报告.docx')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值