Guest User

split excel data

a guest
Dec 8th, 2021
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.40 KB | None | 0 0
  1. import openpyxl
  2. from openpyxl.workbook import workbook
  3. from openpyxl import load_workbook
  4.  
  5. wb = load_workbook('test.xlsx')
  6. sheet = wb.sheetnames # adds sheet names to variable sheet
  7. ws = wb[sheet[0]] # activates the first sheet
  8.  
  9. #Delete A-D
  10. ws.delete_cols(1,4)
  11. print('Columns A-D Deleted')
  12. #Delete K-X
  13. ws.delete_cols(11,14)
  14. print('Columns K-X Deleted')
  15. #Delete L-O
  16. ws.delete_cols(12,4)
  17. print('Columns L-O Deleted')
  18. #Delete N-P
  19. ws.delete_cols(14,3)
  20. print('Columns N-P Deleted')
  21. #Delete O-P
  22. ws.delete_cols(15,2)
  23. print('Columns O-P Deleted')
  24. #Delete P
  25. ws.delete_cols(16,1)
  26. print('Column P Deleted')
  27. #Delete R-AI
  28. ws.delete_cols(18,18)
  29. print('Columns R-AI Deleted')
  30. #Delete S-W
  31. ws.delete_cols(19,5)
  32. print('Columns S-W Deleted')
  33.  
  34. print('Renaming Headings')
  35. ws['N1'].value = 'Contract End'
  36. # print(ws['AM1'].value)
  37.  
  38. ws['P1'].value = 'Has FTTP'
  39. #print(ws['AR1'].value)
  40.  
  41. ws['Q1'].value = 'Greenfield'
  42. #print(ws['AS1'].value)
  43.  
  44. ws['R1'].value = 'TV Customer'
  45. #print(ws['BL1'].value)
  46.  
  47. print('Moving Columns')
  48.  
  49. ws.insert_cols(1, amount=1)
  50.  
  51. col_p = ws['P']
  52. for idx, cell in enumerate(col_p,1):
  53.     ws.cell(row = idx, column = 1).value = cell.value
  54.  
  55. ws.delete_cols(16, 1)
  56.  
  57. ws.insert_cols(15, amount=2)
  58.  
  59. col_fttp = ws['R']
  60. for idx, cell in enumerate(col_fttp,1):
  61.     ws.cell(row = idx, column = 15).value = cell.value
  62.  
  63. col_greenfield = ws['S']
  64. for idx, cell in enumerate(col_greenfield, 1):
  65.     ws.cell(row=idx, column=16).value = cell.value
  66. print('Cleaning Up')
  67. ws.delete_cols(18, 2)
  68. ws.delete_cols(19, 1)
  69.  
  70. number_of_sheets = int(input("How many Sheets: "))
  71. total_rows = ws.max_row-1 # counts the total rows with data
  72. total = int(total_rows / number_of_sheets)
  73. total_cols = ws.max_column
  74.  
  75. wb.save("test.xlsx")
  76. print('Data Completed')
  77. print('--------------------------------------------------')
  78. print(f"You Have a Total of {total_rows} records")
  79. print('--------------------------------------------------')
  80. print(f"You have {total} of records for each day")
  81. print('--------------------------------------------------')
  82. while x < number_of_sheets:
  83.     wb.create_sheet("Sheet_" + str(x))
  84.     x = x + 1
  85.     for row in ws.iter_rows(min_row=1, max_col=total_cols, max_row=total):
  86.         for cell in row:
  87.  
  88.  
  89.         names = ws.cell(row=i, column=2)  # this specifies which column we want to check and output
  90.         name2 = names
  91.         ws.cell(row=i, column=4).value = name2
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
Advertisement
Add Comment
Please, Sign In to add comment