如何用python群发工资条

这是我参与11月更文挑战的第5天,活动详情查看:2021最后一次更文挑战

前言

1
2
3
复制代码每逢工资发放日,便是财务痛苦时~~
没啥文采,不过上面这句话却道出了人在不停重复劳作时,一种痛苦的心态。
身为程序猿的我们,这个时候当然要挺身而出啦,话不多说,撸起袖子写吧。

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
python复制代码import openpyxl
from openpyxl import load_workbook
import yagmail
import keyring
from datetime import *

wb = load_workbook("test.xlsx", data_only = True)
sheet = wb.active

yagmail.register("997131679@qq.com", "AAA")
pwd = keyring.get_password("yagmail", "997131679@qq.com")
yag = yagmail.SMTP(user="997131679@qq.com", host="smtp.qq.com", password=pwd)

count=0
table_header="<thead>"
for row in sheet:
count += 1
if count == 1:
for cell in row:
if cell.column != "B":
table_header += f"<th>{cell.value}</th>"
table_header += "</thead>"
continue
else:
row_text = ""
for cell in row:
if cell.column == "B":
continue
row_text += f"<td>{cell.value}</td>"
row_text += "</tr>"
name = row[0].value
email = row[1].value

contents = f"""
<h3>{name}, hello: </h3>
<p>Please check your {date.today().year}-{date.today().month} salary.</p>
<table border="1px solid black">{table_header}{row_text}</table>
"""
yag.send(f"{email}", f"Wenfang limited company{date.today().year}-{date.today().month}-salary status", contents)
print(f"{name}发送完毕")
print("所有发送完毕")

注意点:yagmail.register(“997131679@qq.com“, “AAA”),第二个参数”AAA”是需要去QQ邮箱设置里获取的,步骤见下图所示:

  1. 点击设置在这里插入图片描述
  2. 切换到【账户】一栏,点击开启SMTP
    在这里插入图片描述
    会出现弹层,提示发送信息给对应的账户,短信发送成功后,即可获取到参数”AAA”的值

运行python文件,出现如下结果
在这里插入图片描述

查看邮箱,已发现新邮件
在这里插入图片描述

本文转载自: 掘金

开发者博客 – 和开发相关的 这里全都有

0%