發(fā)工資啦!教你用Python實(shí)現(xiàn)郵箱自動(dòng)群發(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>請(qǐng)查收你在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>請(qǐng)查收你在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)郵箱自動(dòng)群發(fā)工資條的文章就介紹到這了,更多相關(guān)Python自動(dòng)群發(fā)工資條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python操作MySQL數(shù)據(jù)庫的簡單步驟分享
這篇文章主要給大家介紹了關(guān)于Python操作MySQL數(shù)據(jù)庫的簡單步驟,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-04-04
使用PyInstaller將Python程序文件轉(zhuǎn)換為可執(zhí)行程序文件
與py2exe一樣,PyInstaller程序也可以將Python的.py程序文件轉(zhuǎn)換為.exe,并且還有Linux的版本,下面我們就來詳細(xì)看一下如何使用PyInstaller將Python程序文件轉(zhuǎn)換為可執(zhí)行程序文件2016-07-07
python3使用pandas獲取股票數(shù)據(jù)的方法
今天小編就為大家分享一篇python3使用pandas獲取股票數(shù)據(jù)的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
python DataFrame轉(zhuǎn)dict字典過程詳解
這篇文章主要介紹了python DataFrame轉(zhuǎn)dict字典過程詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
python利用datetime模塊計(jì)算程序運(yùn)行時(shí)間問題
這篇文章主要介紹了python利用datetime模塊計(jì)算程序運(yùn)行時(shí)間,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-02-02
Python實(shí)現(xiàn)數(shù)字小寫轉(zhuǎn)大寫的示例詳解
這篇文章主要為大家詳細(xì)介紹了如何利用Python實(shí)現(xiàn)數(shù)字小寫轉(zhuǎn)大寫的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-12-12
python?包之?multiprocessing?多進(jìn)程
這篇文章主要介紹了python?包之?multiprocessing?多進(jìn)程,首先創(chuàng)建一個(gè)進(jìn)程。實(shí)例化?Process?類創(chuàng)建一個(gè)進(jìn)程對(duì)象、然后調(diào)用它的?start?方法即可生成一個(gè)子進(jìn)程,下文更多相關(guān)資料,需要的朋友可以參考一下2022-04-04

