1. 功能介紹:
當然是因為15號發工資啦,每次都是等呀等呀等呀等呀,最后終於等到一張來自Excel的工資條截圖
於是我就希望能夠學以致用試試,但是寫好了又不敢給領導看~哈哈哈,發給大家批評批評
首先,需要一個Excel咯,這里我用的office2019,如果是舊版本的文件就看第60行注釋
軟件功能就是,可以讀取一個Excel文件並將每一條信息發送到相應的郵箱中
要發送的信息很方便改動
2. python代碼:
這里將html代碼和python分開放了,顯得沒那么臃腫,使用的時候將24行字符串填進去即可
import _thread
import smtplib
import webbrowser
from email.header import Header
from email.mime.text import MIMEText
from tkinter import *
from tkinter import filedialog, messagebox
import pandas as pd
class App:
# 全部
all_info: int = 0
# 發送失敗
err_info: int = 0
# 發送成功
suc_info: int = 0
# 當前數據
t: pd.DataFrame = None
# 表示正在執行的郵件發送線程的數量
counter: int = 0
# html模板,為了防止打包打不進去,干脆寫進來了
html: str = """"""
def __init__(self, _root):
self.root = _root
self.center_window()
# 布局
# line_0
self.path = StringVar()
# self.path.set("請將路徑復制到此框或通過右邊按鈕選擇路徑")
self.path.set("C:/Users/xy/Desktop/工資.xlsx")
self.path_e = Entry(self.root, textvariable=self.path, width=72)
self.path_e.grid(row=0, column=0, columnspan=3, padx=5, pady=5)
self.path_button = Button(self.root, text='選擇路徑', command=self.select_path)
self.path_button.grid(row=0, column=3, padx=5, pady=5)
# line_1
self.info = Listbox(self.root, width=82)
self.info.grid(row=1, column=0, padx=5, pady=5, columnspan=4)
# line_2
self.show_table_b = Button(self.root, text='預覽文件', command=self.show_table)
self.show_table_b.grid(row=2, column=1, padx=5, pady=5)
self.send_email_b = Button(self.root, text='發送郵件', command=self.send)
self.send_email_b.grid(row=2, column=2, padx=5, pady=5, sticky='w')
# line_3
self.msg = StringVar()
self.msg.set('郵件發送狀態')
self.msb_l = Label(self.root, textvariable=self.msg)
self.msb_l.grid(row=3, column=0, columnspan=3, padx=5, pady=5, sticky='w')
# # 獲取html模板
# with open('a.html', 'r') as f:
# self.html = f.read()
def select_path(self):
path_ = filedialog.askopenfilename(title='打開工資表', filetypes=[('Excel', '*.xlsx')])
# 如果要打開舊版本Excel文件,可以將下面這一行取消注釋。順便將上面一行注釋掉。也可以自己寫完整的文件后綴。
# path_ = filedialog.askopenfilename(title='打開工資表')
if path_ != "":
self.path.set(path_)
# 窗口居中,有bug,在電腦設置了“‘縮放與布局-->更改文本、應用等項目的大小’”不為100%時會有偏差。跟分辨率和像素有關,無心解決
def center_window(self, w=600, h=280):
ws = self.root.winfo_screenwidth()
hs = self.root.winfo_screenheight()
x = (ws // 2) - (w // 2)
y = (hs // 2) - (h // 2)
self.root.geometry(f'{w}x{h}+{x}+{y}')
# 就是通過瀏覽器讀取一下Excel信息,應用空間很小,估計只有在電腦上沒有安裝Excel才能用到吧~
def show_table(self):
# 讀Excel
if self.get_data():
return
self.all_info = self.t.index.size
self.t.to_html('a.html')
webbrowser.open('a.html')
# 讀取並發送。
def send(self):
# 讀Excel
if self.get_data():
return
self.all_info = self.t.index.size
# 防止按鈕被點壞了
self.send_email_b['state'] = 'disable'
for index, row in self.t.iterrows():
# 多線程防止界面假死
_thread.start_new_thread(self.send_email, (row.values,))
# 發送郵件並更新狀態
def send_email(self, data: list):
self.counter += 1
# 替換模板內容
html = self.html
for index, item in enumerate(data):
html = html.replace('{' + str(index) + '}', str(item))
receiver = data[-1]
# 發送郵箱的用戶名和授權碼(不是登錄郵箱的密碼)
username = 'tianxiezijideyouxiangzhanghao@qq.com'
password = 'haiyouzijidemima'
# 郵件主題
mail_title = '工資發放情況'
# 郵件內容, 格式, 編碼
message = MIMEText(html, 'html', 'utf-8')
message['From'] = u'小言<' + username + '>'
message['To'] = receiver
message['Subject'] = Header(mail_title, 'utf-8')
try:
smtp = smtplib.SMTP()
smtp.connect('smtp.qq.com')
smtp.login(username, password)
smtp.sendmail(username, receiver, message.as_string())
smtp.quit()
state = '成功'
self.suc_info += 1
except smtplib.SMTPException as e:
state = str(e)
self.err_info += 1
# 添加狀態
self.info.insert('end', data[0].ljust(10, '-') + receiver.ljust(25, '-') + state)
self.counter -= 1
if self.counter == 0:
self.send_email_b['state'] = 'normal'
# 更新信息
self.msg.set(
f'郵件發送狀態:共計 {self.all_info} 條信息,'
f'已發送{self.suc_info + self.err_info}條,'
f'剩余{self.all_info - self.suc_info - self.err_info}條,'
f'其中成功發送{self.suc_info}條,'
f'失敗{self.err_info}條')
def get_data(self):
try:
self.t = pd.read_excel(self.path.get())
self.t.fillna('', inplace=True)
return False
except Exception as e:
messagebox.showerror('出現異常', str(e))
return True
if __name__ == '__main__':
# 寫的不規范,但是無奈不知道規范的怎么寫~
root = Tk()
# 設置全局字體,在四處尋求無果后於百度知道找到了一位大佬在2011年寫出的答案
root.option_add('*Font', 'Fira 10')
root.title('通過郵件發放工資表')
root.resizable(0, 0)
App(root)
root.mainloop()
3. html代碼
<!-- Row Highlight Javascript -->
<script>
window.onload = function () {
var tfrow = document.getElementById('tfhover').rows.length;
var tbRow = [];
for (var i = 1; i < tfrow; i++) {
tbRow[i] = document.getElementById('tfhover').rows[i];
tbRow[i].onmouseover = function () {
this.style.backgroundColor = '#ffffff';
};
tbRow[i].onmouseout = function () {
this.style.backgroundColor = '#f0c169';
};
}
};
</script>
<style type="text/css">
table.tftable {
font-size: 12px;
color: #333333;
width: 100%;
border-width: 1px;
border-color: #ebab3a;
border-collapse: collapse;
}
table.tftable th {
font-size: 12px;
background-color: #e6983b;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #ebab3a;
text-align: left;
}
table.tftable tr {
background-color: #f0c169;
}
table.tftable td {
font-size: 12px;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #ebab3a;
}
</style>
<table id="tfhover" class="tftable" border="1">
<tr>
<th>姓名</th>
<th>崗級</th>
<th>考勤小計</th>
<th>崗位工資,績效獎金</th>
<th>餐補</th>
<th>出差補助</th>
<th>司齡</th>
<th>學歷</th>
<th>證書</th>
<th>話補</th>
<th>地區補貼</th>
<th>C獎金</th>
<th>B獎金</th>
<th>A獎金</th>
<th>D獎金</th>
<th>E獎金</th>
<th>F獎金</th>
<th>工資扣款</th>
<th>應發工資</th>
<th>社保</th>
<th>公積金</th>
<th>個稅</th>
<th>實發工資</th>
<th>郵箱</th>
</tr>
<tr>
<td>{0}</td>
<td>{1}</td>
<td>{2}</td>
<td>{3}</td>
<td>{4}</td>
<td>{5}</td>
<td>{6}</td>
<td>{7}</td>
<td>{8}</td>
<td>{9}</td>
<td>{10}</td>
<td>{11}</td>
<td>{12}</td>
<td>{13}</td>
<td>{14}</td>
<td>{15}</td>
<td>{16}</td>
<td>{17}</td>
<td>{18}</td>
<td>{19}</td>
<td>{20}</td>
<td>{21}</td>
<td>{22}</td>
<td>{23}</td>
</tr>
</table>
最后是Excel表格的樣式,我就不上傳文件了,反正沒有具體的特殊要求
姓名 | 崗級 | 考勤小計 | 崗位工資,績效獎金 | 餐補 | 出差補助 | 司齡 | 學歷 | **證書 | 話補 | 地區補貼 | A獎 | B獎 | C獎 | D獎 | E獎 | F獎 | 工資扣款 | 應發工資 | 社保 | 公積金 | 個稅 | 實發工資 | 郵箱 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
小言 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3293232930@qq.com |
涼雲 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 3293232930@qq.com |