Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. from openpyxl import load_workbook
  2. from openpyxl.styles import Border, Side, Font, colors
  3. from openpyxl.styles.borders import BORDER_THIN
  4.  
  5.  
  6. # file_name = input('file name: ')
  7. file_name = 'TEMPLATE.xlsx'
  8. untouched_wb = load_workbook(file_name)
  9. untouched_ws = untouched_wb.active
  10.  
  11. all_rows = untouched_ws.rows
  12. f_column = untouched_ws['F']
  13. i_column = untouched_ws['I']
  14. j_column = untouched_ws['J']
  15. ft_bold = Font(bold=True)
  16. thin_allborder = Border(bottom=Side(border_style=BORDER_THIN, color='00000000'),
  17. top=Side(border_style=BORDER_THIN, color='00000000'))
  18. ft_red = Font(color=colors.RED)
  19. ignore_indexes = []
  20. offset = 0
  21.  
  22.  
  23. def reddify(index):
  24. f_column[index].font = ft_red
  25. i_column[index].font = ft_red
  26. j_column[index].font = ft_red
  27.  
  28.  
  29. def insert_total(start_index, stop_index, ignore_list):
  30. total = 0
  31. for index_here in range(start_index, stop_index+1):
  32. if index_here in ignore_list:
  33. continue
  34. total += j_column[index_here].value
  35. return total
  36.  
  37.  
  38. for index, each_name in enumerate(f_column):
  39. if each_name.value and each_name.value.lower() != 'name':
  40. present_value = each_name.value
  41. prev_value = f_column[index - 1].value
  42. next_value = f_column[index + 1].value
  43. if present_value != prev_value and present_value != next_value:
  44. if present_value and i_column[index].value and i_column[index].value.lower() != 'subcontractors':
  45. reddify(index)
  46. continue
  47.  
  48. if i_column[index].value.lower() != 'subcontractors':
  49. ignore_indexes.append(index)
  50. # determine start
  51. if present_value != prev_value or not prev_value:
  52. start = index
  53. # determine end
  54. if prev_value and next_value != present_value:
  55. end = index
  56.  
  57. total_amount = insert_total(start, end, ignore_indexes)
  58.  
  59. total_index = end + 2 + offset
  60. untouched_ws.insert_rows(total_index)
  61. untouched_ws[f'J{total_index}'] = total_amount
  62. untouched_ws[f'J{total_index}'].font = ft_bold
  63. untouched_ws[f'J{total_index}'].border = thin_allborder
  64.  
  65. for indexey in ignore_indexes:
  66. reddify(indexey)
  67. offset += 1
  68. ignore_indexes = []
  69.  
  70.  
  71.  
  72. untouched_wb.save(f'new_{file_name}')
  73. print(f'>>SAVED NEW FILE!!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement