目錄
- 一、excel的內(nèi)容
- 二、效果
- 三、需要用的庫:
- 四、實(shí)現(xiàn)步驟
- 4.1 獲取excel表的數(shù)據(jù)
- 4.2 編寫郵件內(nèi)容
- 4.3 發(fā)送郵件
- 五、所有代碼
一、excel的內(nèi)容
二、效果
三、需要用的庫:
- openpyxl
- smptlib
- email.mime.text
- email.header
四、實(shí)現(xiàn)步驟
4.1 獲取excel表的數(shù)據(jù)
wb = load_workbook('數(shù)據(jù)表.xlsx')
sheet = wb.active
for row in sheet:
for cell in row:
print(cell.value)
4.2 編寫郵件內(nèi)容
使用字符串拼接成html
for row in sheet:
tbody = 'tr>'
cnt += 1
if cnt == 1:
for cell in row:
thead += f'th>{cell.value}/th>'
thead += '/thead>'
else:
for cell in row:
tbody += f'td>{cell.value}/td>'
tbody += '/tr>'
name = row[0].value
mail = row[1].value
# 2.編寫郵件內(nèi)容
content = f'''
h3>{name},你好/h3>
p>請查收你在2025年 5月1日 - 5月31 日的工資/p>
table border='1px solid black'>
{thead}
{tbody}
/table>
'''
4.3 發(fā)送郵件
# 發(fā)送郵件
class Test:
def ck_log(self):
pass
def send_email(self, econtent, ename, mail):
host = 'smtp.qq.com'
user = '你的郵箱'
password = '你的授權(quán)碼'
receivers = [mail]
subject = '員工工資表'
msg = MIMEText(econtent, 'html', 'utf-8')
msg['From'] = Header('有限公司')
msg['To'] = Header(ename)
msg['Subject'] = Header(subject, 'utf-8')
try:
obj = smtplib.SMTP_SSL(host, 465)
obj.login(user, password)
obj.sendmail(user, receivers, msg.as_string())
print("郵件發(fā)送成功!")
except smtplib.SMTPException as e:
print("Error: 無法發(fā)送郵件")
print(e)
五、所有代碼
from openpyxl import load_workbook
import smtplib
from email.mime.text import MIMEText
from email.header import Header
'''
1.獲取excel表的數(shù)據(jù)
2.編寫郵件內(nèi)容
3.發(fā)送郵件
'''
# 發(fā)送郵件
class Test:
def ck_log(self):
pass
def send_email(self, econtent, ename, mail):
host = 'smtp.qq.com'
user = '1479898695@qq.com'
password = 'bijoplffwqqlbaci'
receivers = [mail]
subject = '員工工資表'
msg = MIMEText(econtent, 'html', 'utf-8')
msg['From'] = Header('有限公司')
msg['To'] = Header(ename)
msg['Subject'] = Header(subject, 'utf-8')
try:
obj = smtplib.SMTP_SSL(host, 465)
obj.login(user, password)
obj.sendmail(user, receivers, msg.as_string())
print("郵件發(fā)送成功!")
except smtplib.SMTPException as e:
print("Error: 無法發(fā)送郵件")
print(e)
if __name__ == '__main__':
wb = load_workbook('數(shù)據(jù)表.xlsx')
o = Test()
cnt = 0
sheet = wb.active
thead = 'thead>'
# 1.獲取excel表的數(shù)據(jù)
for row in sheet:
tbody = 'tr>'
cnt += 1
if cnt == 1:
for cell in row:
thead += f'th>{cell.value}/th>'
thead += '/thead>'
else:
for cell in row:
tbody += f'td>{cell.value}/td>'
tbody += '/tr>'
name = row[0].value
mail = row[1].value
# 2.編寫郵件內(nèi)容
content = f'''
h3>{name},你好/h3>
p>請查收你在2025年 5月1日 - 5月31 日的工資/p>
table border='1px solid black'>
{thead}
{tbody}
/table>
'''
# 3.發(fā)送郵件
if cnt == 3:
print('content:', content)
print(name, mail)
o.send_email(content, name, mail)
到此這篇關(guān)于發(fā)工資啦!教你用Python實(shí)現(xiàn)郵箱自動群發(fā)工資條的文章就介紹到這了,更多相關(guān)Python自動群發(fā)工資條內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 十個(gè)Python自動化常用操作,即拿即用
- 教你怎么用Python實(shí)現(xiàn)自動生日祝福
- 如何用 Python 子進(jìn)程關(guān)閉 Excel 自動化中的彈窗
- python自動化之如何利用allure生成測試報(bào)告
- 教你怎么用Python處理excel實(shí)現(xiàn)自動化辦公
- Python+uiautomator2實(shí)現(xiàn)自動刷抖音視頻功能
- Python 制作自動化翻譯工具
- python實(shí)現(xiàn)百度文庫自動化爬取
- 用python自動生成日歷
- python調(diào)用stitcher類自動實(shí)現(xiàn)多個(gè)圖像拼接融合功能
- python實(shí)現(xiàn)自動下載sftp文件
- 使用Python自動化Microsoft Excel和Word的操作方法
- python 自動刷新網(wǎng)頁的兩種方法