發(fā)工資啦!教你用Python實(shí)現(xiàn)郵箱自動(dòng)群發(fā)工資條
一、excel的內(nèi)容
二、效果
三、需要用的庫(kù):
- 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: 無(wú)法發(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: 無(wú)法發(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)文章
一文教你學(xué)會(huì)使用Python中的多處理模塊
Python?多處理模塊是一個(gè)強(qiáng)大的工具,用于實(shí)現(xiàn)并行處理,提高程序的性能和效率,本文將詳細(xì)介紹?Python?中多處理模塊的使用方法,希望對(duì)大家有所幫助2024-01-01Python虛擬環(huán)境庫(kù)virtualenvwrapper安裝及使用
這篇文章主要介紹了Python虛擬環(huán)境庫(kù)virtualenvwrapper安裝及使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06python3+PyQt5重新實(shí)現(xiàn)自定義數(shù)據(jù)拖放處理
這篇文章主要為大家詳細(xì)介紹了python3+PyQt5重新實(shí)現(xiàn)自定義數(shù)據(jù)拖放處理,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04python中時(shí)間轉(zhuǎn)換datetime和pd.to_datetime詳析
這篇文章主要給大家介紹了關(guān)于python中時(shí)間轉(zhuǎn)換datetime和pd.to_datetime的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08通過(guò)python將大量文件按修改時(shí)間分類的方法
今天小編就為大家分享一篇通過(guò)python將大量文件按修改時(shí)間分類的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-10-10Python?類中定義多個(gè)構(gòu)造器方法重載與泛方法
這篇文章主要為大家介紹了Python?類中定義多個(gè)構(gòu)造器方法重載與泛方法示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03