zoukankan      html  css  js  c++  java
  • 生成随机串码并保存到Excel中

    import string,time,os
    import random
    from openpyxl import load_workbook,Workbook
    from openpyxl.styles import Alignment
    from openpyxl.styles import PatternFill
    from openpyxl.styles import Side,Border
    
    
    def genPassword(length):
        chars = string.ascii_lowercase + string.digits
        return ''.join(random.sample(chars,length))  #返回的字符串中没有重复的字符
        # return ''.join(random.choice(chars)  for i in range(length))  #返回的字符串中有重复的字符
    
    def writeTxt(file_name,length=3):
        date = time.strftime('%Y%m', time.localtime())
        t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        password = 'dag' + date
        password += genPassword(length)
        tplt = "{0}	{1}
    ".format(t,password)
        with open(file_name,'a+') as f:
            f.write(tplt)
    
    def write_Excel(file_name,username,password,length=3):
        password = password+ genPassword(length)
        t = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        if os.path.exists(file_name):
            #打开存在的excel表
            workbook = load_workbook(file_name)
            sheet = workbook.active
        else:
            #创建新的excel表
            workbook = Workbook()
            sheet = workbook.active
        print(sheet.max_row)
        if(sheet.max_row == 1 and sheet.max_column == 1):  #判断是否为空表
            header = ['序号', '用户名', '密码', '生成时间']
            sheet.append(header)
        #print(sheet.max_row)
        no = sheet.max_row
        username = username
        #添加一整行
        row_lst = [no,username,password,t]
        sheet.append(row_lst)
    
        #调整列宽 Alignment
        sheet.column_dimensions['C'].width = 20
        sheet.column_dimensions['D'].width = 20
        sheet.row_dimensions[1].height = 30 #设置首行
        #设置其他行
        for i in range(2,sheet.max_row+1):
            sheet.row_dimensions[i].height = 20
    
        #填充单元格,PatternFill
        # 设置边框,Side,Border
        # 设置对齐, Alignment
        pattern_fill = PatternFill(fill_type='solid',fgColor='cccccc')
        side = Side(style='thin',color='000000')
        border = Border(left=side,right=side,top=side,bottom=side)
        alignment = Alignment(horizontal='center',vertical='center',wrap_text=True)
        #设置首行
        for row in sheet.iter_rows(min_row=1,max_row=1,min_col=1,max_col=4):
            for cell in row:
                cell.fill = pattern_fill #填充
                cell.border = border #边框
                cell.alignment = alignment #对齐
        #设置其他行
        for row in sheet.iter_rows(min_row=2,max_row=sheet.max_row,min_col=1,max_col=4):
            for cell in row:
                cell.border = border #边框
                cell.alignment = alignment #对齐
    
        #保存
        workbook.save(file_name)
    
    def main():
        root = os.getcwd() + "/"
        file_name = 'password.xlsx'
        full_file_name = root + file_name
        #writeTxt(full_file_name)
        date = time.strftime('%Y%m', time.localtime())
        username = 'admin'
        try:
            iptName = str(input("请输入用户名[默认为admin]:"))
            if iptName:
                username = iptName
        except:
            print('')
        password = username + date
        try:
            length = int(input("密码固定前缀为:{}
    请输入密码后缀长度[默认长度为3]:".format(password)))
        except:
            length = 3
        write_Excel(full_file_name,username,password,length)
        os.startfile(full_file_name)
    
    main()
    
  • 相关阅读:
    css区分ie6,7,ff
    轮播插件--可支持视频拖拽和可视区播放
    json无限树----几个月前写的插件
    WEBGL学习笔记二
    记录一些坑
    webGL学习笔记一
    Angular模态框
    Angular指令实践之type等于text的input星号输入
    Angular内置指令
    Angular指令一
  • 原文地址:https://www.cnblogs.com/yuexiao/p/14788647.html
Copyright © 2011-2022 走看看