add new fun to generate profiles
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
import json
|
||||
import random
|
||||
import string
|
||||
|
||||
import pandas as pandas
|
||||
import xlsxwriter
|
||||
|
||||
from definitions import CONTACT_LIST_FILE
|
||||
from pojo.contact_pojo import ContactPojo
|
||||
|
||||
phone_number_prefix = ['6', '7']
|
||||
|
||||
|
||||
class ExcelHelper:
|
||||
|
||||
@@ -36,3 +42,92 @@ class ExcelHelper:
|
||||
mail=contact_dict['email'])
|
||||
contact_list.append(contact)
|
||||
return contact_list
|
||||
|
||||
def read_names(self, file_name=CONTACT_LIST_FILE) -> list:
|
||||
contact_list_in_json = pandas.read_excel(file_name).to_json(orient='records')
|
||||
contact_dict_list = json.loads(contact_list_in_json)
|
||||
contact_list = []
|
||||
count = 2
|
||||
for contact_dict in contact_dict_list:
|
||||
if contact_dict['name']:
|
||||
raw_name = contact_dict['name'].strip()
|
||||
name = raw_name.split(' ')
|
||||
if len(name) == 1:
|
||||
name = raw_name.split('\xa0')
|
||||
last_name = name[0]
|
||||
if len(name) == 2:
|
||||
first_name = name[-1]
|
||||
else:
|
||||
first_name = ''.join(name[1:len(name)])
|
||||
|
||||
contact = ContactPojo(phone_number="",
|
||||
last_name=last_name,
|
||||
first_name=first_name,
|
||||
passport_number="",
|
||||
mail="")
|
||||
|
||||
if len(first_name) == 0:
|
||||
print("first_name is empty: position:" + str(count))
|
||||
print(name)
|
||||
if len(last_name) == 0:
|
||||
print("last_name is empty: position:" + str(count))
|
||||
count = count + 1
|
||||
contact_list.append(contact)
|
||||
|
||||
return contact_list
|
||||
|
||||
|
||||
def get_random_phone_numbers():
|
||||
length = 8 # number of characters in the string.
|
||||
ran = ''.join(random.choices(string.digits, k=length))
|
||||
id_number = random.choice(phone_number_prefix) + str(ran)
|
||||
return id_number
|
||||
|
||||
|
||||
def generate_email_from_name(first_name: str, last_name: str) -> str:
|
||||
length = 3 # number of characters in the string.
|
||||
ran = ''.join(random.choices(string.digits, k=length))
|
||||
email = "{}{}{}@163.com".format(last_name.lower(), first_name.lower(), ran)
|
||||
print(email)
|
||||
return email
|
||||
|
||||
|
||||
def get_random_id_number() -> str:
|
||||
# write_the_valid_profiles_to_excel()
|
||||
S = 8 # number of characters in the string.
|
||||
# call random.choices() string module to find the string in Uppercase + numeric data.
|
||||
ran = ''.join(random.choices(string.digits, k=S))
|
||||
print("The randomly generated string is : 94" + str(ran)) # print the random data
|
||||
return ran
|
||||
|
||||
|
||||
def write_new_contacts_to_excel(valid_contacts: list):
|
||||
row = 0
|
||||
col = 0
|
||||
# Create a workbook and add a worksheet.
|
||||
workbook = xlsxwriter.Workbook('new_profile_{}.xlsx'.format(len(valid_contacts)))
|
||||
header_data = ['name', 'phone', 'passport', 'email']
|
||||
worksheet = workbook.add_worksheet()
|
||||
header_format = workbook.add_format({'bold': True})
|
||||
|
||||
for col_num, data in enumerate(header_data):
|
||||
worksheet.write(row, col_num, data, header_format)
|
||||
row = row + 1
|
||||
for info in valid_contacts:
|
||||
info.phone = get_random_phone_numbers()
|
||||
info.passport = get_random_id_number()
|
||||
info.mail = generate_email_from_name(info.first_name, info.last_name)
|
||||
# Iterate over the data and write it out row by row.
|
||||
worksheet.write(row, col, "{} {}".format(info.last_name, info.first_name))
|
||||
worksheet.write(row, col + 1, info.phone)
|
||||
worksheet.write(row, col + 2, info.passport)
|
||||
worksheet.write(row, col + 3, info.mail)
|
||||
row += 1
|
||||
workbook.close()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
excel_reader = ExcelHelper()
|
||||
contacts = excel_reader.read_names("new_profile_500.xlsx")
|
||||
print(contacts)
|
||||
# write_new_contacts_to_excel(valid_contacts=contacts)
|
||||
|
||||
Reference in New Issue
Block a user