Advertisement
MohamedShamil

JD4

Nov 19th, 2024 (edited)
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.20 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. import os
  6. # importing everything from tkinter
  7. from tkinter import *
  8. from tkinter import scrolledtext
  9. from docx import Document
  10.  
  11. # create gui window
  12. Main_window = Tk()
  13.  
  14. # set the configuration
  15. # of the window
  16. w = 400 # width for the Tk root
  17. h = 200 # height for the Tk root
  18.  
  19. # get screen width and height
  20. ws = Main_window.winfo_screenwidth() # width of the screen
  21. hs = Main_window.winfo_screenheight() # height of the screen
  22.  
  23. # calculate x and y coordinates for the Tk root window
  24. x = (ws/2) - (w/2)
  25. y = (hs/2) - (h/2)
  26.  
  27. # set the dimensions of the screen
  28. # and where it is placed
  29. Main_window.geometry('%dx%d+%d+%d' % (w, h, x, y))
  30. Main_window.resizable(0, 0)
  31. #my_label = Label(Main_window,
  32. #                 text = "geeksforgeeks")
  33.  
  34. jd_out_path = './Finished JDs'
  35. temp_jd_out_path = './Finished JDs/temp'
  36.  
  37. try:
  38.     os.mkdir(jd_out_path) #create JD out directory
  39.     os.mkdir(temp_jd_out_path) #create JD out directory
  40. except:
  41.     print("JD directory already exists")
  42.  
  43. template_1 = "jd_template.docx"
  44. label_text="...."
  45.  
  46. textw = scrolledtext.ScrolledText(Main_window,width=47,height=12)
  47. textw.grid(column=1, row=2,sticky=N+S+E+W)
  48.    
  49.  
  50. Main_window.title("JD Maker 1.0 - MTO")
  51.  
  52. # Show a simple example
  53. csv_path = 'jd.csv'
  54.  
  55.  
  56. from docx import Document
  57.  
  58. def delete_bullet_points(file, text_to_delete):
  59.     # Load the document
  60.     doc = Document(temp_jd_out_path + "/" + file)
  61.  
  62.     for table in doc.tables:
  63.         for row in table.rows:
  64.             for cell in row.cells:
  65.                 for paragraph in cell.paragraphs:
  66.                     if text_to_delete in paragraph.text:
  67.                          paragraph.text = ''
  68.                          p = paragraph._element
  69.                          p.getparent().remove(p)
  70.     # Save the document with the changes
  71.     doc.save(jd_out_path + "/" + file)
  72.  
  73.  
  74.  
  75. def Fill_JD():
  76.     Main_window.update_idletasks()
  77.     try:
  78.         # do something
  79.  
  80.         df= pd.read_csv(csv_path)
  81.         data=df.to_dict('records')
  82.        
  83.         #convert to str
  84.        
  85.         list_delim, tuple_delim = '-', '^'
  86.         for item in data:
  87.             res = dict()
  88.             for sub in item:
  89.                 # checking data types
  90.                 if isinstance(item[sub], list):
  91.                     res[sub] = list_delim.join([str(ele) for ele in item[sub]])
  92.                 elif isinstance(item[sub], tuple):
  93.                     res[sub] = tuple_delim.join(list([str(ele) for ele in item[sub]]))
  94.                 else:
  95.                     res[sub] = str(item[sub])
  96.             item = res
  97.             # --- end convert to str ---
  98.             document_1 = MailMerge(template_1)
  99.             document_1.merge(**res)
  100.        
  101.         #for item in data:
  102.             #print(item)  
  103.             #print("ITEM: ")
  104.             #print(item['name_english'])
  105.             label_text = "Now processing JD for: " + item['name_english']
  106.             textw.insert(END, label_text)
  107.             Main_window.update_idletasks()
  108.            
  109.             try:
  110.                 csv2_path =  item['name_english'] + '.csv'
  111.                 #df2=pd.read_csv(csv2_path)
  112.                
  113.                 jd_tasks= pd.read_csv(csv2_path).to_dict('records')
  114.  
  115.                 for k in jd_tasks:
  116.                     k['job_task_marks'] = str(k['job_task_marks'])
  117.  
  118.                 document_1.merge_rows('job_task_marks', jd_tasks)
  119.                 # Save the document as example 1
  120.                 document_1.write(temp_jd_out_path + "/" +  item['name_english'] + '_jd.docx')
  121.                 textw.insert(END, " -->(Done)\n")
  122.                
  123.                 #Clean Up Document
  124.                 # Example usage
  125.                 file = item['name_english'] + '_jd.docx'
  126.                 line_to_delete = 'nan'
  127.                 delete_bullet_points(file, line_to_delete)
  128.                
  129.             except Exception as e:
  130.                 textw.insert(END, " -->(Error)\n")
  131.     except Exception as e:
  132.         # handle it
  133.         raise Exception("Kommis kamah nibei vege. " + str(e))
  134.         textw.insert(END, " -->(Error)\n")
  135.    
  136.  
  137. Fill_JD()
  138. Main_window.mainloop()
  139.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement