【Python】自动化办公 篇四——python生成工作周报

利用前面自动化办公的所有知识,实现了生成工作周报,代码量也不是很多,主要是应用了前面的很多知识,还是很开心的呢

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!')

至此自动化办公的部分先告一段落啦,后期我还是会将自动化办公的更多升级版的功能再次的完善好上传上来的,加油啦!!嘻嘻,如有不对的地方,敬请指正,谢谢!

猜你喜欢

转载自blog.csdn.net/weixin_44566432/article/details/107861623