備忘記錄
#! /usr/bin/python3
# coding=utf-8
import os
import xlwt
import xlrd
import smtplib
card_list = []
def show_menu():
? ? """顯示菜單"""
? ? print("*" * 50)
? ? print("")
? ? print("歡迎使用 【 工資條發(fā)送系統(tǒng) 】 V 1.0")
? ? print("")
? ? print("選擇? 【1】? 新增人員工資條")
? ? print("選擇? 【2】? 顯示全部名單")
? ? print("選擇? 【3】? 搜索工資條")
? ? print("選擇? 【4】? 發(fā)送到郵箱")
? ? print("")
? ? print("選擇? 【0】? 退出系統(tǒng)")
? ? print("")
? ? print("*" * 50)
def new_card():
? ? """
新增名冊信息
? ? """
? ? print("-" * 50)
? ? print("新增人員工資條")
? ? # 1.提示用戶輸入名片信息
? ? email_str = input("請輸入郵箱:")
? ? name_str = input("請輸入姓名:")
? ? gz_str = input("請輸入基本工資:")
? ? cq_str = input("請輸入本月出勤天數(shù):")
? ? sf_str = input("請輸入本月實發(fā)工資:")
? ? # 2.使用戶輸入的信息建立一個名片字典
? ? card_dict = {"name": name_str,
? ? ? ? ? ? ? ? ?"gzi": gz_str,
? ? ? ? ? ? ? ? ?"chuq": cq_str,
? ? ? ? ? ? ? ? ?"email": email_str + "@qq.com",
? ? ? ? ? ? ? ? ?"shif": sf_str}
? ? # 3.將名片字典添加到列表中
? ? card_list.append(card_dict)
? ? # print(card_list) #打印名單出來看一下
? ? # 4.提示用戶添加成功
? ? print("添加%s的工資條成功!" % name_str)
def show_all():
? ? """
顯示所有人員信息
? ? :return: 把值返回給函數(shù)
? ? """
? ? print("-" * 50)
? ? print("顯示所有人員工資條信息")
? ? if len(card_list) == 0:
? ? ? ? print("當(dāng)前沒有任何人員工資條信息記錄,請使用新增功能添加!")
? ? ? ? return
? ? # 打印表頭
? ? for name in ["郵箱", "姓名", "基本工資", "出勤天數(shù)", "實發(fā)工資"]:
? ? ? ? print(name, end="\t")
? ? print("")
? ? # 打印分割線
? ? print("=" * 50)
? ? # 遍歷列表依次輸出字典信息
? ? for card_dict in card_list:
? ? ? ? print("%s\t%s\t%s\t%s\t%s\t" % (card_dict["email"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["name"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["gzi"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["chuq"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["shif"]))
def search_card():
? ? """
搜索人員工資條信息信息
? ? """
? ? print("-" * 50)
? ? print("人員工資條信息")
? ? # 1.提示用戶輸入要搜索的姓名
? ? find_name = input("請輸入要搜索的姓名:")
? ? # 2.遍歷名片列表查詢要搜索的姓名,如果沒有找到需要提示用戶
? ? for card_dict in card_list:
? ? ? ? if card_dict["name"] == find_name:
? ? ? ? ? ? print("郵箱\t\t姓名\t基本工資\t出勤天數(shù)\t實發(fā)工資")
? ? ? ? ? ? print("=" * 50)
? ? ? ? ? ? print("%s\t%s\t%s\t%s\t%s\t" % (card_dict["email"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["name"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["gzi"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["chuq"],
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? card_dict["shif"]))
? ? ? ? ? ? # 針對找到的名片記錄執(zhí)行修改和刪除的操作
? ? ? ? ? ? deal_card(card_dict)
? ? ? ? ? ? break
? ? else:
? ? ? ? print("抱歉沒有找到%s" % find_name)
def deal_card(find_dict):
? ? """
處理查找到的人員
? ? :param find_dict: 查找到的人員
? ? """
? ? action_str = input("請選擇要執(zhí)行的操作 "
? ? ? ? ? ? ? ? ? ? ? ?"[1] 修改 [2] 刪除 [0] 返回上一級")
? ? if action_str == "1":
? ? ? ? find_dict["email"] = input_card_info(find_dict["email"], "郵箱:")
? ? ? ? find_dict["name"] = input_card_info(find_dict["name"], "姓名:")
? ? ? ? find_dict["gzi"] = input_card_info(find_dict["gzi"], "基本工資:")
? ? ? ? find_dict["chuq"] = input_card_info(find_dict["chuq"], "出勤天數(shù):")
? ? ? ? find_dict["shif"] = input_card_info(find_dict["shif"], "實發(fā)工資:")
? ? ? ? print("修改信息成功")
? ? elif action_str == "2":
? ? ? ? card_list.remove(find_dict)
? ? ? ? print("刪除信息成功!")
def input_card_info(dict_value, tip_message):
? ? """
輸入人員信息
? ? :param dict_value: 字典中原有的值
? ? :param tip_message: 輸入的提示文字
? ? :return: 如果用戶輸入了內(nèi)容,就返回內(nèi)容,否則返回字典中原有的值
? ? """
? ? result_str = input(tip_message)
? ? if len(result_str) > 0:
? ? ? ? return result_str
? ? else:
? ? ? ? return dict_value
def w_excel_cards():
? ? """
導(dǎo)出信息到Excel
? ? """
? ? # wb = xlwt.Workbook()
? ? wb = xlwt.Workbook(encoding='utf-8')
? ? ws = wb.add_sheet("員工工資表")
? ? # 3個參數(shù)分別為行號,列號,和內(nèi)容
? ? # 需要注意的是行號和列號都是從0開始的
? ? ws.write(0, 0, "郵箱")
? ? ws.write(0, 1, "姓名")
? ? ws.write(0, 2, "基本工資")
? ? ws.write(0, 3, "出勤天數(shù)")
? ? ws.write(0, 4, "實發(fā)工資")
? ? # 循環(huán)遍歷寫入Excel
? ? excel_row = 1
? ? for card_dict in card_list:
? ? ? ? ws.write(excel_row, 0, card_dict["email"])
? ? ? ? ws.write(excel_row, 1, card_dict["name"])
? ? ? ? ws.write(excel_row, 2, card_dict["gzi"])
? ? ? ? ws.write(excel_row, 3, card_dict["chuq"])
? ? ? ? ws.write(excel_row, 4, card_dict["shif"])
? ? ? ? excel_row += 1
? ? # 設(shè)置單元格寬度,也就是某一列的寬度
? ? ws.col(0).width = 5555
? ? ws.col(1).width = 2222
? ? ws.col(2).width = 2222
? ? ws.col(3).width = 2222
? ? ws.col(4).width = 2222
? ? # 保存excel文件
? ? wb.save('./員工工資表.xlsx')
? ? # print("導(dǎo)出數(shù)據(jù)成功?。?!")
def r_excel_cards():
? ? file_path = "./員工工資表.xlsx"
? ? # 先打開一個文件
? ? wb = xlrd.open_workbook(file_path)
? ? # 獲取第一個表
? ? sheet1 = wb.sheet_by_index(0)
? ? # 總行數(shù)
? ? nrows = sheet1.nrows
? ? # 后面就通過循環(huán)即可遍歷數(shù)據(jù)了
? ? # 取數(shù)據(jù)
? ? for i in range(1, nrows):
? ? ? ? email_str = sheet1.cell_value(i, 0)
? ? ? ? name_str = sheet1.cell_value(i, 1)
? ? ? ? gz_str = sheet1.cell_value(i, 2)
? ? ? ? cq_str = sheet1.cell_value(i, 3)
? ? ? ? sf_str = sheet1.cell_value(i, 4)
? ? ? ? # 2.使用戶輸入的信息建立一個名片字典
? ? ? ? card_dict = {"name": name_str,
? ? ? ? ? ? ? ? ? ? ?"gzi": gz_str,
? ? ? ? ? ? ? ? ? ? ?"chuq": cq_str,
? ? ? ? ? ? ? ? ? ? ?"email": email_str,
? ? ? ? ? ? ? ? ? ? ?"shif": sf_str}
? ? ? ? # 3.將名片字典添加到列表中
? ? ? ? card_list.append(card_dict)
def email_qq():
? ? from email.mime.text import MIMEText? # 郵件正文
? ? from email.header import Header? # 郵件頭
? ? # 加載Excel文件
? ? wb = xlrd.open_workbook("員工工資表.xlsx")
? ? sheet = wb.sheet_by_index(0)
? ? user_name = input("請輸入您自己的郵箱號:")
? ? user_pass = input("請輸入郵箱密碼:")
? ? # 登錄郵箱
? ? smtp_obj = smtplib.SMTP()
? ? smtp_obj.connect("smtp.qq.com")
? ? smtp_obj.login(user_name + "@qq.com", user_pass)
? ? # 循環(huán)遍歷Excel
? ? count = 0
? ? table_col_html = '<thead>'? # 表頭
? ? for row in sheet:? # sheet.iter_rows(min_row=0)
? ? ? ? count += 1
? ? ? ? if count == 1:? # 第一行
? ? ? ? ? ? for col in row:
? ? ? ? ? ? ? ? table_col_html += f"<th>{col.value}</th>"
? ? ? ? ? ? table_col_html += "</thead>"
? ? ? ? ? ? # print(table_col_html)
? ? ? ? ? ? continue
? ? ? ? else:
? ? ? ? ? ? row_text = "<tr>"? # 開始一行
? ? ? ? ? ? for cell in row:
? ? ? ? ? ? ? ? row_text += f"<td>{cell.value}</td>"
? ? ? ? ? ? row_text += "</tr>"? # 結(jié)束一行
? ? ? ? ? ? name = row[1]
? ? ? ? ? ? staff_email = row[0].value
? ? ? ? ? ? # print(name.value, staff_email)
? ? ? ? mail_body_context = f'''
? ? ? ? ? ? <h3>{name.value}\t你好:</h3>
? ? ? ? ? ? <p>請查收您本月工資條,如有疑問電話聯(lián)系</p>
? ? ? ? ? ? <table border="1px solid black">
? ? ? ? ? ? ?{table_col_html}
? ? ? ? ? ? {row_text}
? ? ? ? ? ?</table>
? ? ? ? '''
? ? ? ? msg_body = MIMEText(mail_body_context, "html", "utf-8")
? ? ? ? msg_body["From"] = Header("xxx公司人事部", "utf-8")? # 發(fā)送者
? ? ? ? msg_body["To"] = Header("xxx公司員工", "utf-8")? # 接受者
? ? ? ? msg_body["Subject"] = Header("xxx公司本月工資", "utf-8")? # 主題
? ? ? ? # 發(fā)郵件
? ? ? ? smtp_obj.sendmail(user_name + "@qq.com", [staff_email, ], msg_body.as_string())
? ? ? ? print(f"成功發(fā)送 {name.value} 的工資條到 {staff_email} 中.....")
if not os.access("./員工工資表.xlsx", os.F_OK):
? ? wb = xlwt.Workbook(encoding='utf-8')
? ? ws = wb.add_sheet("員工工資表")
? ? # 3個參數(shù)分別為行號,列號,和內(nèi)容
? ? # 需要注意的是行號和列號都是從0開始的
? ? ws.write(0, 0, "郵箱")
? ? ws.write(0, 1, "姓名")
? ? ws.write(0, 2, "基本工資")
? ? ws.write(0, 3, "出勤天數(shù)")
? ? ws.write(0, 4, "實發(fā)工資")
? ? wb.save('./員工工資表.xlsx')
else:
? ? r_excel_cards()
while True:
? ? #? 顯示菜單
? ? show_menu()
? ? action_str = input("請選擇希望執(zhí)行的操作:")
? ? print("您選擇的操作是 【%s】" % action_str)
? ? if action_str in ["1", "2", "3", "4"]:
? ? ? ? if action_str == "1":
? ? ? ? ? ? new_card()
? ? ? ? elif action_str == "2":
? ? ? ? ? ? show_all()
? ? ? ? elif action_str == "3":
? ? ? ? ? ? search_card()
? ? ? ? elif action_str == "4":
? ? ? ? ? ? w_excel_cards()
? ? ? ? ? ? email_qq()
? ? elif action_str == "0":
? ? ? ? print("歡迎再次使用 【 工資條發(fā)送系統(tǒng) 】")
? ? ? ? w_excel_cards()
? ? ? ? break
? ? else:
? ? ? ? print("您輸入的序號不正確請重新輸入!")