Guest User

exceler

a guest
Jul 30th, 2017
224
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.33 KB | None | 0 0
  1. #!/usr/bin/python3
  2. ##############################################################################
  3. # imports required for the script to work
  4. import openpyxl
  5. from openpyxl.styles import NamedStyle, Font, Border, Side, PatternFill
  6. import sys
  7. import time
  8. import os
  9. import glob
  10. ###############################################################################
  11.  
  12. os.chdir(".")       # bound path to the current directory
  13.  
  14. #satisfy openpyxl requirements for highlighting cells
  15. tf = NamedStyle(name="tf")
  16. tf.font = Font(bold=False, size=18)
  17. bd = Side(style='thick', color="000000")
  18. tf.border = Border(left=bd, top=bd, right=bd, bottom=bd)
  19. tf.fill = PatternFill(fill_type='lightUp',
  20.                  start_color='fff000',
  21.                 end_color='6efdfd')
  22.  
  23. print("\n")
  24. print("files avilable in this folder:")
  25. print("\n")
  26.  
  27. #enum files in current directory
  28. files = os.listdir()
  29. i = 1
  30. for f in glob.glob("*.xlsx"):
  31.     print("(" + str(i) + "). "  + str(f))
  32.     i+= 1
  33.  
  34. print("\n")
  35. print("enter the name of your excel file without extension:")
  36. wb_name = input()
  37. print("\n")
  38. print("opening your book...")
  39. time.sleep(1)
  40. try:
  41.     workBook = openpyxl.load_workbook(wb_name + str(".xlsx"))
  42. except IOError:
  43.     print("could not find the book. exiting...")
  44.     sys.exit()
  45. print("I found the following sheets in your book:")
  46. print("\n")
  47. j = 1;
  48. for sheets in workBook.sheetnames:
  49.     print("(" + str(j) + "). "  + str(sheets))
  50.     j += 1
  51. print("\n")
  52. print("enter the sheet name with bank data:")
  53. b_sheet = input()
  54. print("\n")
  55. try:
  56.     bankSheet = workBook.get_sheet_by_name(b_sheet)
  57. except KeyError:
  58.     print("no such sheet in your file: " + wb_name + " xlsx.")
  59.     print("exitting....")
  60.     sys.exit()
  61.  
  62. print("SUCESS: bank data found at sheet: " + b_sheet)
  63. print("\n")
  64. print("enter the sheet name with your data:")
  65. u_sheet = input()
  66. try:
  67.     userSheet = workBook.get_sheet_by_name(u_sheet)
  68.     print("\n")
  69.     print("SUCESS: user data found at sheet: " + u_sheet)
  70.     print("\n")
  71. except KeyError:
  72.     print("no such sheet found in your file: " + wb_name + " xlsx.")
  73.     sys.exit()
  74.  
  75. #loop through all records in Column B of the excel file and convert them
  76. #into an array. return that array
  77. def get_stuff(sheetName):
  78.     stuff = []
  79.     for row in range(2, sheetName.max_row + 1):
  80.         cellObj = sheetName["B" + str(row)]
  81.         eachChq = cellObj.value
  82.         stuff.append(eachChq)
  83.     return stuff
  84.  
  85. bank_IDS = get_stuff(bankSheet)
  86.  
  87.  
  88. print("processing your data....")
  89. time.sleep(2)
  90. print("finding matches...")
  91. time.sleep(2)
  92. count = 0   #keep track of matches found
  93. for row in range(2, userSheet.max_row + 1):
  94.     IDSObject = userSheet["B" + str(row)]
  95.     leavesObj = userSheet["C" + str(row)]
  96.    
  97.     if IDSObject.value in bank_IDS:
  98.         bankSheet["B" + str(row)].style = tf
  99.         bankSheet["C" + str(row)].value = leavesObj.value
  100.         count += 1
  101. print(str(count) + " matches found")
  102. print("\n")
  103. print("hold on...")
  104. time.sleep(1)
  105. print("highlighting in process...")
  106. time.sleep(2)
  107. print("SUCCESS:" + str(count) + " matches highlighted")
  108. time.sleep(1)
  109. print("creating new file in your folder....")
  110. time.sleep(1)
  111. workBook.save("ready.xlsx")             # create new file with all the matched instance highlighted automatically
  112. print("ready.xlsx created")
  113. time.sleep(2)
  114. print("Exiting...")
Add Comment
Please, Sign In to add comment