Python读取两份excel中的所有学生成绩,通过matplotlib将两份表格分别生成以10为区间的分数段两个直方图,将学生两次成绩进行对比并两次成绩颜色不相同的直方图,所有直方图是以横坐标为分数、区间为10,纵坐标为人数、区间为1,直方图有边框,将生成的所有直方图保存为图片文件,通过pandas分析统计数据,结合数据生成一段描述学生成绩升降的文字,最后将生成的文字和所有图片保存到新建的word中,在程序中应基于图形用户界面实现,通过菜单About (关于) 进行查看
时间: 2023-06-10 07:08:31 浏览: 81
以下是Python实现该功能的代码:
```python
import pandas as pd
import matplotlib.pyplot as plt
from docx import Document
from docx.shared import Inches
from tkinter import *
from tkinter import filedialog
# 打开文件选择器选择两份表格
def choose_file():
file_path1 = filedialog.askopenfilename(filetypes=[('Excel files', '*.xlsx')])
file_path2 = filedialog.askopenfilename(filetypes=[('Excel files', '*.xlsx')])
if file_path1 and file_path2:
# 读取数据
df1 = pd.read_excel(file_path1)
df2 = pd.read_excel(file_path2)
# 合并数据
df = pd.concat([df1, df2], axis=1)
# 统计数据
count1, bins1, _ = plt.hist(df1['score'], bins=range(0, 110, 10), edgecolor='black')
count2, bins2, _ = plt.hist(df2['score'], bins=range(0, 110, 10), edgecolor='black')
# 画图
plt.clf() # 清空图像
plt.hist(df1['score'], bins=range(0, 110, 10), edgecolor='black', color='blue', alpha=0.5)
plt.hist(df2['score'], bins=range(0, 110, 10), edgecolor='black', color='red', alpha=0.5)
plt.xlabel('Score')
plt.ylabel('Number of Students')
plt.title('Comparison of Scores')
plt.legend(['First Time', 'Second Time'])
plt.savefig('histogram.png')
# 生成文本
if count1.mean() > count2.mean():
text = 'The overall performance of the students has decreased.'
elif count1.mean() < count2.mean():
text = 'The overall performance of the students has improved.'
else:
text = 'The overall performance of the students has remained the same.'
# 生成Word文档
document = Document()
document.add_heading('Comparison of Scores', 0)
document.add_picture('histogram.png', width=Inches(6))
document.add_paragraph(text)
document.save('report.docx')
# 显示完成提示
label.config(text='Report generated successfully.')
else:
label.config(text='Please select two files.')
# 创建GUI界面
root = Tk()
root.title('Grade Comparison')
root.geometry('300x100')
button = Button(root, text='Choose Files', command=choose_file)
button.pack(pady=10)
label = Label(root, text='')
label.pack()
root.mainloop()
```
该程序使用了pandas库读取表格数据,matplotlib库绘制直方图,docx库生成Word文档。通过GUI界面选择两份表格,统计数据并绘制直方图,生成文本和Word文档,最后在界面上显示完成提示。