Advertisement
MohamedShamil

Untitled

Nov 19th, 2024
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.67 KB | None | 0 0
  1. from __future__ import print_function
  2. import pandas as pd
  3. from mailmerge import MailMerge
  4. from datetime import date
  5.  
  6. #get all csv files in the current directory
  7. #import os
  8. #path = os.getcwd()
  9. #extension = '.csv'
  10. #for root, dirs_list, files_list in os.walk(path):
  11. #    for file_name in files_list:
  12. #        if os.path.splitext(file_name)[-1] == extension:
  13. #            file_name_path = os.path.join(root, file_name)
  14. #            print(file_name)
  15. #            print(file_name_path)   # This is the full path of the filter file
  16.  
  17. # this is the extension you want to detect
  18. #extension = '.csv'
  19. # Define the templates - assumes they are in the same directory as the code
  20. template_1 = "jd_template.docx"
  21.  
  22. # Show a simple example
  23. csv_path = 'jd.csv'
  24.  
  25. try:
  26.     # do something
  27.     df=pd.read_csv(csv_path)
  28.     for idx,row in df.iterrows():
  29.         global data
  30.         data={
  31.             '[name_english]': row['name_english'],
  32.             '[name]': row['name'],
  33.             '[present_address]': row['present_address'],
  34.             '[phone]': row['phone'],
  35.             '[id_card_no]': row['id_card_no'],
  36.             '[date_of_birth]': row['date_of_birth'],
  37.             '[permenant_address]': row['permenant_address'],
  38.             '[emergency_contact_name]': row['emergency_contact_name'],
  39.             '[emergency_contact_phone]': row['emergency_contact_phone'],
  40.             '[job_no]': row['job_no'],
  41.             '[job_name_and_rank]': row['job_name_and_rank'],
  42.             '[job_purpose]': row['job_purpose'],
  43.             '[unit]': row['unit'],
  44.             '[jd_date]': row['jd_date'],
  45.             '[basic_salary]': row['basic_salary'],
  46.             '[service_allowance]': row['service_allowance'],
  47.             '[supporting_co_allowance]': row['supporting_co_allowance'],
  48.             '[name_2]': row['name_2'],
  49.             '[job_title]': row['job_title'],
  50.             '[sign_date]': row['sign_date']
  51.         }
  52.         print("Data Row is:" + data['[name]'])  
  53.         document_1 = MailMerge(template_1)
  54.         print("Fields included in {}: {}".format(template_1,
  55.                                                 document_1.get_merge_fields()))
  56.         # Merge in the values
  57.         document_1.merge(
  58.             name=data['[name]'],
  59.             present_address=data['[present_address]'],
  60.             phone=str(data['[phone]']),
  61.             id_card_no=data['[id_card_no]'],
  62.             date_of_birth=data['[date_of_birth]'],
  63.             permenant_address=data['[permenant_address]'],
  64.             emergency_contact_name=data['[emergency_contact_name]'],
  65.             emergency_contact_phone=str(data['[emergency_contact_phone]']),
  66.             job_no=str(data['[job_no]']),
  67.             job_name_and_rank=data['[job_name_and_rank]'],
  68.             job_purpose=data['[job_purpose]'],
  69.             unit=data['[unit]'],
  70.             jd_date=data['[jd_date]'],
  71.             basic_salary=str(data['[basic_salary]']),
  72.             service_allowance=str(data['[service_allowance]']),
  73.             supporting_co_allowance=str(data['[supporting_co_allowance]']),
  74.             name_2=data['[name_2]'],
  75.             job_title=data['[job_title]'],
  76.             sign_date=data['[sign_date]'],
  77.         )
  78.  
  79.         csv2_path =  data['[name_english]'] + '.csv'
  80.         df2=pd.read_csv(csv2_path)
  81.         jd_tasks= pd.read_csv(csv2_path).to_dict('records')
  82.  
  83.         for k in jd_tasks:
  84.             k['job_task_marks'] = str(k['job_task_marks'])
  85.  
  86.         document_1.merge_rows('job_task_marks', jd_tasks)
  87.         # Save the document as example 1
  88.         document_1.write(data['[name_english]'] + '_jd.docx')
  89.  
  90. except Exception as e:
  91.     # handle it
  92.     raise Exception("Kommis kamah nibei vege. " + str(e))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement