Advertisement
Guest User

Untitled

a guest
Jun 1st, 2018
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.26 KB | None | 0 0
  1. import openpyxl
  2. from tqdm import tqdm
  3. import smtplib
  4. from email.mime.application import MIMEApplication
  5. from email.mime.multipart import MIMEMultipart
  6. from email.mime.text import MIMEText
  7. from email.utils import COMMASPACE, formatdate
  8. from os.path import basename
  9.  
  10. '''
  11. This script takes all loans in the "Master Wire" excel file and splits them into different worksheets
  12. by their funding cost center. Then, it takes those new worksheets and emails them to the appropriate
  13. manager based on the funding cost centers they manage.
  14. '''
  15.  
  16. filename = 'test_master_wire.xlsx'
  17. wb = openpyxl.load_workbook(filename=filename, data_only=True, keep_vba=True)
  18. all_sheets = wb.get_sheet_names()
  19. funding_cost_center_dict= {}
  20. all_fcc = []
  21. all_loan_dict = {}
  22. headers = []
  23. gmail_user = 'xxxxxxx@guildmortgage.net'
  24. gmail_password = 'xxxxxxxxx'
  25.  
  26. print ('getting emails...')
  27. #create dict with funding cost centers and emails from email list sheet
  28. def get_emails(funding_cost_center_dict):
  29.     email_list_sheet = wb.get_sheet_by_name('Wire File Contacts')
  30.     for rownum in range(2, email_list_sheet.max_row+1):
  31.         fcc = email_list_sheet.cell(row=rownum, column=1).value
  32.         funding_cost_center_dict[fcc] = []
  33.         ops_mgr_emails = email_list_sheet.cell(row=rownum, column=2).value
  34.         funding_mgr_emails = email_list_sheet.cell(row=rownum, column=3).value
  35.         def split_semis(emails):
  36.             if ';' in emails:
  37.                 emails = emails.split(';')
  38.                 for email in emails:
  39.                     email = email.strip()
  40.                     funding_cost_center_dict[fcc].append(email)
  41.             else:
  42.                 funding_cost_center_dict[fcc].append(emails)
  43.         split_semis(ops_mgr_emails)
  44.         split_semis(funding_mgr_emails)
  45.     return funding_cost_center_dict
  46. email_dict = get_emails(funding_cost_center_dict)
  47.  
  48. #############################################################################
  49.  
  50. print ('creating new worksheets based off funding cost center...')
  51. #split loans from master wire sheet into separate excel files based on fcc
  52. def split_loans(all_fcc, all_loan_dict, headers):
  53.     master_wire_sheet = wb.get_sheet_by_name('Super Wire File')
  54.     print ('getting unique funding cost centers...')
  55.     pbar = tqdm(total=master_wire_sheet.max_row)
  56.     for rownum in range(2, master_wire_sheet.max_row+1):
  57.         pbar.update(1)
  58.         current_loan = []
  59.         fcc = master_wire_sheet.cell(row=rownum, column=26).value
  60.         all_fcc.append(fcc)
  61.     pbar.close()
  62.     all_fcc = set(all_fcc)
  63.     for fcc in all_fcc:
  64.         all_loan_dict[fcc] = []
  65.     #get headers into list
  66.     for col in range(1,526):
  67.         headers.append(master_wire_sheet.cell(row=1, column=col).value)
  68.     print ('adding all loans to python dict...')
  69.     pbar = tqdm(total=master_wire_sheet.max_row)
  70.     for rownum in range(2, master_wire_sheet.max_row+1):
  71.         pbar.update(1)
  72.         fcc = master_wire_sheet.cell(row=rownum, column=26).value
  73.         for col in range(1,525+1):
  74.             current_loan.append(master_wire_sheet.cell(row=rownum, column=col).value)
  75.         all_loan_dict[fcc].append(current_loan)
  76.         current_loan = []
  77.     pbar.close()
  78.     wb.close()
  79. split_loans(all_fcc, all_loan_dict, headers)
  80.  
  81. #create worksheets with headers for every FCC
  82. def create_worksheets(all_fcc):
  83.     for fcc in all_fcc:
  84.         new_filename = str(fcc) + ' Workbook.xlsx'
  85.         try:
  86.             new_wb = openpyxl.load_workbook(filename=new_filename, data_only=True)
  87.         except:
  88.             new_wb = openpyxl.Workbook()
  89.         new_ws = new_wb.active
  90.         for h in range(0, len(headers)):
  91.             new_ws.cell(row=1, column=h+1).value = headers[h]
  92.         new_wb.save(new_filename)
  93.         new_wb.close()
  94. all_fcc = set(all_fcc)
  95. create_worksheets(all_fcc)
  96.  
  97. #get row incrementor to paste loans
  98. def count_rows(all_fcc):
  99.     row_counter_dict = {}
  100.     for fcc in all_fcc:
  101.         row_counter_dict[fcc] = 2
  102.     return row_counter_dict
  103. row_counter_dict = count_rows(all_fcc)
  104.  
  105. print ('updating all new workbooks...')
  106. #adding all to dictionary
  107. def add_loans_to_workbooks(all_loan_dict, row_counter_dict):
  108.     total = 0
  109.     for key,val in all_loan_dict.items():
  110.         for v in val:
  111.             total += 1
  112.     pbar = tqdm(total=total)
  113.     for key, val in all_loan_dict.items():
  114.         #update spreadsheet
  115.         new_fcc = key
  116.         new_filename = str(new_fcc) + ' Workbook.xlsx'
  117.         new_wb = openpyxl.load_workbook(filename=new_filename, data_only=True)
  118.         for v in val:
  119.             pbar.update(1)
  120.             new_ws = new_wb.active
  121.             new_max_row = row_counter_dict[new_fcc]
  122.  
  123.             for col in range(1,526):
  124.                 new_ws.cell(row=new_max_row, column=col).value = v[col-1]
  125.             row_counter_dict[new_fcc] += 1
  126.  
  127.         new_wb.save(new_filename)
  128.         new_wb.close()
  129.     pbar.close()
  130. add_loans_to_workbooks(all_loan_dict, row_counter_dict)
  131.  
  132. print ('Sending emails...')
  133. #send an email to each manager responsible for the fccs
  134. def send_emails(funding_cost_center_dict, gmail_user, gmail_password):
  135.     pbar = tqdm(total=len(funding_cost_center_dict))
  136.     for fcc, email_list in funding_cost_center_dict.items():
  137.         pbar.update(1)
  138.         for email in email_list:
  139.             msg = MIMEMultipart()
  140.             msg['From'] = 'xxxxxxx@guildmortgage.net'
  141.             msg['To'] = email
  142.             msg['Subject'] = 'Tester Message'
  143.             text = 'Hello, please review and send back asap'
  144.  
  145.             msg.attach(MIMEText(text))
  146.             attach_file = str(fcc) + ' Workbook.xlsx'
  147.             with open(attach_file, "rb") as fil:
  148.                 part = MIMEApplication(
  149.                     fil.read(),
  150.                     Name=basename(attach_file)
  151.                 )
  152.             # After the file is closed
  153.             part['Content-Disposition'] = 'attachment; filename="%s"' % basename(attach_file)
  154.             msg.attach(part)
  155.  
  156.             server = smtplib.SMTP_SSL('smtp.gmail.com', 465)
  157.             server.ehlo()
  158.             server.login(gmail_user, gmail_password)
  159.             server.sendmail('xxxxxxx@guildmortgage.net', email, msg.as_string())
  160.             server.close()
  161.     pbar.close()
  162. send_emails(funding_cost_center_dict, gmail_user, gmail_password)
  163.  
  164. print ('Complete.')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement