python实现从excel存取svn账号密码并发送邮件通知

2018-11-28  本文已影响60人  CUFFS

python实现从excel存取svn账号密码并发送邮件通知

功能初衷

准备工作

源码分析

任意目录创建pw_gen.py文件

# -*- coding:utf8 -*-
from openpyxl import Workbook
from openpyxl import load_workbook
import sys
import string
import smtplib
from email.mime.text import MIMEText
from email.header import Header
from email.mime.multipart import MIMEMultipart
from pypinyin import pinyin, lazy_pinyin, Style
import random
# https://www.cnblogs.com/sun-haiyu/p/7096423.html

reload(sys)
sys.setdefaultencoding('utf-8')


def usage():
    '''usage'''
    print 'Usage: %s %s' % (
        sys.argv[0], 'genpwd|sendmail')
    sys.exit()


def load_mail():
    wb = load_workbook('./txl3.xlsx')
    # ws = wb["员工信息表1"]
    ws = wb.active
    for row_num in range(4, 30):
        row_num_str = str(row_num)
        B_name = ws['B'+row_num_str].value
        E_username = ws['E'+row_num_str].value
        F_password = ws['F'+row_num_str].value
        H_mail = ws['H'+row_num_str].value
        content = "用户名:"+E_username+"密码:"+F_password+"\n"+"访问地址:http://127.0.01:1080/*,*替换成具体的项目路径"
        
        print content
        print B_name
        print H_mail
        send_mail(B_name, H_mail, content)


def send_mail(name, mail, content):
    # 第三方 SMTP 服务
    mail_host = "smtp.163.com"  # 设置服务器
    mail_user = "***@163.com"  # 用户名
    mail_pass = "*****"  # 口令

    sender = '***@163.com'
    receivers = mail  # 接收邮件,可设置为你的QQ邮箱或者其他邮箱
    # message = MIMEMultipart()
    message = MIMEText(content, 'plain', 'utf-8')
    # result=open(file, 'rb').read()
    # message.attach(MIMEText(result, 'plain', 'utf-8'))
    message['From'] = sender
    message['To'] = receivers
    # message['To'] = ','.join(receivers)
    subject = name+'SVN信息改变通知'
    message['Subject'] = Header(subject, 'utf-8')
    try:
        smtpObj = smtplib.SMTP()
        smtpObj.connect(mail_host, 25)    # 25 为 SMTP 端口号
        smtpObj.login(mail_user, mail_pass)
        smtpObj.sendmail(sender, receivers, message.as_string())
        print "邮件发送成功"
    except Exception, e:
        print "邮件发送失败"
        print str(e)


def save_excel():
    wb = load_workbook('./txl3.xlsx')
    # ws = wb["员工信息表1"]
    ws = wb.active
    print ws
    for row_num in range(4, 30):
        row_num_str = str(row_num)
        B_row_value = ws['B'+row_num_str].value
        cell_value = ws['C'+row_num_str].value
        third_str = cell_value[-4:]
        s = lazy_pinyin(B_row_value)
        username = ''.join(s)
        first_str = username.title()
        # 生成密码
        second_str = ''.join(random.sample('#@$!', 3))
        pwd = first_str+second_str+third_str
        print row_num_str
        print pwd
        print username
        # 保存svn用户名
        ws['E'+row_num_str] = username
        # 保存svn密码
        ws['F'+row_num_str] = pwd
    wb.save('./txl3.xlsx')


def main():
    if len(sys.argv) != 2:
        usage()
    operations = sys.argv[1]
    if operations == "genpwd":
        save_excel()
    elif operations == "sendmail":
        print operations
        load_mail()
    else:
        print "操作参数不正确"


if __name__ == '__main__':
    main()

操作命令

#生成svn账号和密码保存到excel
python pw_gen.py genpwd
#从excel读取svn账号密码发送到员工邮箱
python pw_gen.py sendmail
上一篇下一篇

猜你喜欢

热点阅读