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

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

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值