如何用python群发工资条

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

前言

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

代码实现

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("[email protected]", "AAA")
pwd = keyring.get_password("yagmail", "[email protected]")
yag = yagmail.SMTP(user="[email protected]", 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("[email protected]", "AAA"),第二个参数"AAA"是需要去QQ邮箱设置里获取的,步骤见下图所示:

  1. 点击设置在这里插入图片描述
  2. 切换到【账户】一栏,点击开启SMTP

在这里插入图片描述 会出现弹层,提示发送信息给对应的账户,短信发送成功后,即可获取到参数"AAA"的值

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

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

Guess you like

Origin juejin.im/post/7035099821874610183