一、需求場景:
網(wǎng)站建設哪家好,找成都創(chuàng)新互聯(lián)公司!專注于網(wǎng)頁設計、網(wǎng)站建設、微信開發(fā)、微信小程序、集團企業(yè)網(wǎng)站建設等服務項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了赫山免費建站歡迎大家使用!
1、需要系統(tǒng)自動注冊某賬號時候,要按照規(guī)范編寫Excel,再導入到系統(tǒng)中。
2、需要手工核對,或者使用七拼八湊的各網(wǎng)站功能生成結(jié)果再粘貼進Excel整理。
二、想法:
使用Python的openpyxl模塊一次性完成表的填寫
三、實踐:
# -*- coding: utf-8 -*- import time import random import string import names from openpyxl import Workbook wb = Workbook() current_time = time.strftime('%Y-%m-%d_%H%M%S') dest_filename = 'Ali' + current_time + '.xlsx' ws = wb.active num = input("請輸入需要生成的賬號數(shù)量:") if num.isdigit(): num = int(num) ## print("初始化數(shù)據(jù)......") column_name = [ '賬號用戶名', '密碼', '網(wǎng)站代碼', '手機號', '手機任務機code', 'pc任務機code', '支付類型', '標簽名稱', '代理方式', '代理IP', '代理端口號', '代理用戶名', '代理密碼', '代理協(xié)議', '支付賬號卡號', '支付賬號綁定方式', '購買方案名稱', '狀態(tài)', '初始化狀態(tài)', '推手手機號', '是否可銷售', '是否可評論', '賬號類型', '名', '姓', '性別', '生日', '郵箱', '國家', '洲/省', '城市', '郵編', '詳細地址', '收件人名', '收件人姓', '收件人電話', '地址使用方式', '注冊帳號狀態(tài)', '分組名稱', '支付賬號使用金額', '地區(qū)代碼', '機房名稱', '手機代理方式' ] email_list = ['@hotmail.com', '@outlook.com', '@msn.com', '@gmail.com'] src = string.ascii_letters + string.digits c_col = "aliexpress" g_col = "信用卡" i_col = aq_col = "L-US-FLOW" n_col = "socks5" digits = string.digits p_col = "固定" q_col = "擺布系統(tǒng)測試方案" r_col = "啟用" s_col = "初始化成功" t_col = "promoteAdmin1" w_col = "新號" gender = ['male', 'female'] start = (1971, 1, 1, 0, 0, 0, 0, 0, 0) end = (2005, 12, 31, 23, 59, 59, 0, 0, 0) start_time = time.mktime(start) end_time = time.mktime(end) ac_col = "美國" ak_col = "使用地址庫地址" al_col = "未注冊" am_col = "速賣通2" ao_col = [ "Alaska", "Alabama", "Arkansas", "Eastern Samoa", "Arizona", "California", "Colorado", "Connecticut", "Delaware", "Florida", "Georgia", "Guam", "Hawaii", "Idaho", "Illinois", "Indiana", "Kansas", "Kentucky", "Louisiana", "Massachusetts", "Maryland", "Maine", "Marshall Island", "Michigan", "Minnesota", "Missouri", "Mississippi", "Montana", "Iowa", "North Dakota", "Nebraska", "New hampshire", "New jersey", "New mexico", "New York", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode island", "South carolina", "Tennessee", "Texas", "Utah", "Virginia", "Vermont", "Washington", "Wisconsin", "Wyoming", "Nevada", "North Carolina" ] ap_col = "克利夫蘭機房" ## print(f"正在生成數(shù)量為{num}的速賣通賬號,請稍后......",) length = len(column_name) n = 1 for t in column_name: if length: length -= 1 ws.cell(row=1, column=n, value=t) n += 1 ## for _ in range(2, num+2): str1 = random.choice([names.get_last_name(), names.get_first_name()]) str2 = ''.join(random.sample(string.ascii_letters + string.digits, 8)) str3 = random.choice(email_list) strs = str1 + str2 + str3 ws.cell(row=_, column=1, value=strs) # list_passwd_all = random.sample(src, 9) list_passwd_all.extend(random.sample(string.digits, 1)) list_passwd_all.extend(random.sample(string.ascii_lowercase, 1)) list_passwd_all.extend(random.sample(string.ascii_uppercase, 1)) random.shuffle(list_passwd_all) str_passwd = ''.join(list_passwd_all) ws.cell(row=_, column=2, value=str_passwd) # ws.cell(row=_, column=3, value=c_col) ws.cell(row=_, column=7, value=g_col) ws.cell(row=_, column=9, value=i_col) ws.cell(row=_, column=14, value=n_col) # payment_account = '9' + ''.join(random.sample(digits, 9)) ws.cell(row=_, column=15, value=payment_account) # ws.cell(row=_, column=16, value=p_col) ws.cell(row=_, column=17, value=q_col) ws.cell(row=_, column=18, value=r_col) ws.cell(row=_, column=19, value=s_col) ws.cell(row=_, column=20, value=t_col) ws.cell(row=_, column=23, value=w_col) # current_gender = random.choice(gender) name1 = names.get_first_name(gender=current_gender) ws.cell(row=_, column=24, value=name1) ws.cell(row=_, column=34, value=name1) if current_gender == 'male': ws.cell(row=_, column=26, value='男') else: ws.cell(row=_, column=26, value='女') name2 = names.get_last_name() ws.cell(row=_, column=25, value=name2) ws.cell(row=_, column=35, value=name2) # date_touple = time.localtime(random.randint(start_time, end_time)) date = time.strftime("%Y/%m/%d", date_touple) ws.cell(row=_, column=27, value=date) # ws.cell(row=_, column=28, value=ac_col) # 州,城市,郵編,詳細地址,收件人電話,地區(qū)代碼-------未完成 # ws.cell(row=_, column=37, value=ak_col) ws.cell(row=_, column=38, value=al_col) ws.cell(row=_, column=39, value=am_col) # address_code = random.choice(ao_col) ws.cell(row=_, column=41, value=address_code) # ws.cell(row=_, column=42, value=ap_col) ws.cell(row=_, column=43, value=aq_col) try: wb.save(filename=dest_filename) print(f"速賣通賬號生成完畢,請查看當前目錄下的{dest_filename},如果該文件已存在將被覆蓋") except PermissionError as reason: print(reason) print(f"出錯啦!!請保持{dest_filename}為關閉狀態(tài)")
四、結(jié)果演示:
輸入數(shù)量運行后,將生成如Ali2018-12-05_174523.xlsx名字的Excel
值得一提的是,使用了names可以準確地生成名字的“正確”性別。
名 | 姓 | 性別 | 生日 |
George | Rhodarmer | 男 | 2001/11/03 |
Ebony | Trinka | 女 | 1987/05/24 |
Caron | Mitchell | 女 | 1985/09/27 |
Trisha | Bell | 女 | 2002/12/22 |
Maria | Spaulding | 女 | 1973/02/18 |
Dorothy | Noyes | 女 | 1991/01/18 |
Nicholas | Williams | 男 | 1992/06/11 |
Charles | Dougherty | 男 | 1999/04/07 |
Cortney | Moore | 女 | 1997/09/24 |
Pilar | Miller | 女 | 1991/08/04 |
Jayna | Ramirez | 女 | 1973/10/13 |
Annie | Osullivan | 女 | 2003/09/21 |
Leonard | Sapp | 男 | 2001/12/12 |
Joe | Akins | 男 | 1971/09/29 |