Created
August 8, 2018 13:40
-
-
Save srdg/ed6039324517ec58fe8c17dc0bf95f80 to your computer and use it in GitHub Desktop.
Creating csv for Gsuite bulk upload and Mailchimp bulk notifications
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from random import randint | |
import openpyxl | |
parent_str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" | |
ob = openpyxl.load_workbook("College Mail Id for ECE (Responses).xlsx") | |
sh = ob["Form Responses 1"] | |
pwd_log = [] | |
def create_password(): | |
''' | |
Creates password for user | |
''' | |
pwd = '' | |
for _ in range(12): | |
pwd+=parent_str[randint(0,60)] | |
pwd_log.append(pwd+'\n') | |
return pwd | |
def split_name(row=2,column=1): | |
''' | |
Separates first and last name of user i | |
''' | |
name = str(sh.cell(row=row,column=column).value).upper().strip().split(' ') | |
first_name = name[0] | |
last_name = ' '.join(name[1:]) | |
return (first_name,last_name) | |
def get_primary_mail(row=2,column=1,pass_year='20',batch='ece'): | |
''' | |
Creates primary mail address for user i | |
''' | |
first_name,last_name = split_name(row,column) | |
roll = str(sh.cell(row=row,column=2).value)[-2:] | |
new_mail_id = first_name[0].lower()*int(first_name[0]!=' ')+last_name[0].lower()*int(last_name[0]!=' ')+pass_year+roll+'@'+batch+'.jgec.ac.in' | |
return new_mail_id | |
def get_secondary_mail(row=2,column=3): | |
''' | |
Returns mail address for user i | |
''' | |
return str(sh.cell(row=row,column=column).value) | |
def return_output(): | |
''' | |
Returns list of strings to be written in users.csv | |
''' | |
write,string = [],'' | |
for user in range(2,sh.max_row+1): | |
first_name,last_name=split_name(user,1) | |
string+=first_name+',' | |
string+=last_name+',' | |
string+=get_primary_mail(user,1,'20','ece')+',' | |
string+=create_password()+',,' | |
string+='/Students/ECE/2020 batch,' | |
string+=get_secondary_mail(user,3)+',,,,,,,,,,,,,,,,,'+'True\n' | |
write.append(string) | |
string='' | |
return write | |
def main(): | |
''' | |
Writes to users.csv | |
''' | |
output = return_output() | |
with open("users.csv","a+") as f: | |
f.writelines(output) | |
with open("pwd.log","w+") as f: | |
f.writelines(pwd_log) | |
f_in = open("users.csv","r+") | |
f = open("mailchimp.csv","w+") | |
string='' | |
f.write('Email address,FNAME,LNAME,JGECMAIL,PWD\n') | |
for line in f_in: | |
string = line[line.find('batch')+6:line.find(',,,,,,,')]+',' | |
string += line[:line.find(',,')] | |
f.write(string+'\n') | |
f.close() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment