看了一些辦公自動化的文章,感覺更多是知識的羅列,看完后不知所雲。為了更方面大家去學習,今天小編將以學生考試成績為例, 手把手教你使用Python完成自動化辦公,一鍵生成學生成績數據分析報告(word版),完整版代碼,在下面公眾號后台回復:word ,如果對PPT、excel自動化辦公感興趣,可以下方留言。
准備數據
沒有數據,數據分析報告無從談起。
from faker import Faker
import pandas as pd
#生成數據
fake = Faker('zh_CN')
name = []
sex= []
score1 = []
score2 = []
score3 = []
score4 = []
number = range(1,31)
for _ in range(30):
name.append(fake.simple_profile(sex=None)['name'])
sex.append(fake.simple_profile(sex=None)['sex'])
score1.append(random.randint(40,100))
score2.append(random.randint(40,100))
score3.append(random.randint(40,100))
score4.append(random.randint(200,300))
df = pd.DataFrame({
'學號':number,
'姓名':name,
'性別':sex,
'語文':score1,
'數學':score2,
'英語':score3,
'理綜':score4
})
df = df.set_index('學號')
df.to_excel('學生成績單.xlsx')
生成分析報告
考試成績明細
核心代碼
p = document.add_paragraph('本次測評,全班共有{}名同學參加考試,其中分數總分排名第一的同學是'.format(len(students.姓名)),style='Heading 3')
p.add_run(str(first_student)).bold = True
p.add_run(',分數為')
p.add_run(str(first_score)).bold = True
p.add_run('.學生考試總體成績如下')
table = document.add_table(rows=len(students.姓名)+1, cols=6, style='Medium Shading 1 Accent 5')
table.cell(0,0).text = '姓名'
table.cell(0,1).text = '語文'
table.cell(0,2).text = '數學'
table.cell(0,3).text = '英語'
table.cell(0,4).text = '理綜'
table.cell(0,5).text = '總分'
for i,(index,row) in enumerate(students.iterrows()):
table.cell(i+1, 0).text = str(row['姓名'])
table.cell(i+1, 1).text = str(row['語文'])
table.cell(i+1, 2).text = str(row['數學'])
table.cell(i+1, 3).text = str(row['英語'])
table.cell(i+1, 4).text = str(row['理綜'])
table.cell(i+1, 5).text = str(row['總分'])
結果
考試成績匯總
核心代碼
students['總分'] = students.語文 + students.數學 + students.英語 + students.理綜
students.sort_values(by='總分', inplace=True, ascending=False)
students.reset_index(drop=True, inplace=True)
#學生成績匯總表
ax = students.plot.bar(x='姓名', y=['語文','數學','英語','理綜'], stacked=True)
plt.title('學生成績匯總圖', fontsize=16) # fontproperties=font
plt.xlabel('姓名', fontsize=10) # fontproperties=font,
plt.xticks(rotation='45', fontsize=8) # fontproperties=font,
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
plt.tight_layout()
plt.savefig('Part3_data.jpg')
結果展示
各科成績表現
def pie_plot(scores):
ratios=dict()
for subject,subjectScore in scores.items():
ratios[subject]={}
if subject !='理綜':
for category,num in groupby(sorted(subjectScore),splitScore):
ratios[subject][category]= len(tuple(num))
else:
for category,num in groupby(sorted(subjectScore),splitScore_lizong):
ratios[subject][category]= len(tuple(num))
fig ,axs = plt.subplots(2,2)
# 畫子圖
axs.shape=1,4
for index,subjectData in enumerate(ratios.items()):
plt.sca(axs[0][index])
subjectName,subjectRatio = subjectData
plt.pie(list(subjectRatio.values()),labels=list(subjectRatio.keys()),autopct='%1.1f%%')
plt.xlabel(subjectName)
plt.legend(loc="right",bbox_to_anchor=(1, 0, 0.5, 1))
plt.savefig('Part4_data.jpg')
plt.show()
pie_plot(scores)
結果展示
歷次考試成績
上面都是對一個班級整體的數據分析報告。如果希望一份分析報告中, 除了班級整體成績分析外,還想生成一份針對個人成績報告,目的不僅是可以知道這個班級的整體學習狀況,也可以知道每個學生的學習情況,下面我們以一個王歡同學為例:
核心代碼
temp_df = pd.DataFrame({
'歷次考試':number,
'姓名':'王歡',
'語文':score1,
'數學':score2,
'英語':score3,
'理綜':score4
})
plt.subplot(411)
plt.plot(temp_df['語文'],'y*-',label="語文")
plt.subplot(412)
plt.plot(temp_df['數學'],'b*-')
plt.subplot(413)
plt.plot(temp_df['英語'],'r*-')
plt.subplot(414)
plt.plot(temp_df['理綜'])
plt.savefig('Part6_data.jpg')
plt.show()
結果展示
該同學成績概括性分析
成果展示
Python自動化辦公,目的是提升效率,避免重復勞動,同時可以針對每個人的情況,做到千人千面,省時省力,一鍵生成,非常簡單。因視頻不支持,有興趣的可以公眾號上看!
推薦閱讀
技術交流
歡迎轉載、收藏本文,碼字不易,有所收獲點贊支持一下!
為方便進行學習交流,本號開通了技術交流群,添加方式如下:
直接添加小助手微信號:pythoner666,備注:CSDN+python,或者按照如下方式添加均可!