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')
day10-python操作word文档作业
最新推荐文章于 2024-10-06 14:44:47 发布