[Python] Office Automation Chapter Four-Python generates weekly work report

Using all the knowledge of the previous automated office, the work week report was generated, and the amount of code was not a lot, mainly because a lot of the previous knowledge was applied, and I was still very happy.

from docx import Document
import pandas as pd
import matplotlib.pyplot as plt

students = pd.read_excel('excel/1.xlsx')
#sort_values 排序, inplace 原地修改,ascending False从大到小
students.sort_values(by='Score', inplace=True, ascending=False)

#直接使用plt.bar()绘制柱状图
plt.bar(students.Name, students.Score, color='orange')

#设置标题、xy轴名称
plt.title('Student Score', fontsize=16)
plt.xlabel('Name')
plt.ylabel('Score')

#因为x轴字体太长,利用rotation 将其旋转90°, 方便显示
plt.xticks(students.Name, rotation='90')
#紧凑型布局,因为x轴文字较长,为了显示全
plt.tight_layout()
# plt.show()
img_name = 'test.jpg'
plt.savefig(img_name)

document = Document()
document.add_heading('数据分析报告', level=0)#最高级别

first_student = students.iloc[0, :]['Name']
first_score = students.iloc[0, :]['Score']

p = document.add_paragraph('分数排名第一的学生是')
p.add_run(str(first_student)).bold = True
p.add_run(', 分数是:')
p.add_run(str(first_score)).bold = True

p1 = document.add_paragraph(f'总共有{len(students.Name)}名学生参加了考试:')
table = document.add_table(rows=len(students.Name)+1, cols=2)

table.style = 'LightShading-Accent1'
table.cell(0, 0).text = '学生姓名'
table.cell(0, 1).text = '学生分数'

for i, (index, row) in enumerate(students.iterrows()):
    table.cell(i+1, 0).text = str(row['Name'])
    table.cell(i+1, 1).text = str(row['Score'])

document.add_picture(img_name)
document.save('students.docx')
print('Done!')

At this point, the part of automated office has come to an end. Later, I will upload more and more upgraded functions of automated office again. Come on! ! Hee hee, if there is something wrong, please correct me, thank you!

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/107861623