python按需生成固定数量电话号码并保存为excel不重复

47次阅读

共计 872 个字符,预计需要花费 3 分钟才能阅读完成。

如果设置了本地环境变量,可直接执行

#coding=utf-8
# -*- coding: utf-8 -*-
import xlwt
import random
numList = []
#设置表格样式
def set_style(name,height,bold=False):
    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style

def generatorNum():
    list = ['139', '188', '185', '136', '155', '135', '158', '151', '152', '153']
    str = '0123456789'
    for i in range(2000): #在这里改所需数量
        global numList 
        numList.append(random.choice(list) + "".join(random.choice(str) for i in range(8)))

#写 Excel
def write_excel():
    f = xlwt.Workbook()
    sheet1 = f.add_sheet('number',cell_overwrite_ok=True)
    row0 = ["number"]
    global numList
    print(numList)
    colum0 = numList
    #写第一行
    for i in range(0,len(row0)):
        sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))
    #写第一列
    for i in range(0,len(colum0)):
        sheet1.write(i+1,0,colum0[i],set_style('Times New Roman',220,True))

    f.save('test.xls')

if __name__ == '__main__':
    generatorNum()
    write_excel()

正文完
 0