Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import openpyxl
- from openpyxl.workbook import workbook
- from openpyxl import load_workbook
- wb = load_workbook('test.xlsx')
- sheet = wb.sheetnames # adds sheet names to variable sheet
- ws = wb[sheet[0]] # activates the first sheet
- #Delete A-D
- ws.delete_cols(1,4)
- print('Columns A-D Deleted')
- #Delete K-X
- ws.delete_cols(11,14)
- print('Columns K-X Deleted')
- #Delete L-O
- ws.delete_cols(12,4)
- print('Columns L-O Deleted')
- #Delete N-P
- ws.delete_cols(14,3)
- print('Columns N-P Deleted')
- #Delete O-P
- ws.delete_cols(15,2)
- print('Columns O-P Deleted')
- #Delete P
- ws.delete_cols(16,1)
- print('Column P Deleted')
- #Delete R-AI
- ws.delete_cols(18,18)
- print('Columns R-AI Deleted')
- #Delete S-W
- ws.delete_cols(19,5)
- print('Columns S-W Deleted')
- print('Renaming Headings')
- ws['N1'].value = 'Contract End'
- # print(ws['AM1'].value)
- ws['P1'].value = 'Has FTTP'
- #print(ws['AR1'].value)
- ws['Q1'].value = 'Greenfield'
- #print(ws['AS1'].value)
- ws['R1'].value = 'TV Customer'
- #print(ws['BL1'].value)
- print('Moving Columns')
- ws.insert_cols(1, amount=1)
- col_p = ws['P']
- for idx, cell in enumerate(col_p,1):
- ws.cell(row = idx, column = 1).value = cell.value
- ws.delete_cols(16, 1)
- ws.insert_cols(15, amount=2)
- col_fttp = ws['R']
- for idx, cell in enumerate(col_fttp,1):
- ws.cell(row = idx, column = 15).value = cell.value
- col_greenfield = ws['S']
- for idx, cell in enumerate(col_greenfield, 1):
- ws.cell(row=idx, column=16).value = cell.value
- print('Cleaning Up')
- ws.delete_cols(18, 2)
- ws.delete_cols(19, 1)
- number_of_sheets = int(input("How many Sheets: "))
- total_rows = ws.max_row-1 # counts the total rows with data
- total = int(total_rows / number_of_sheets)
- total_cols = ws.max_column
- wb.save("test.xlsx")
- print('Data Completed')
- print('--------------------------------------------------')
- print(f"You Have a Total of {total_rows} records")
- print('--------------------------------------------------')
- print(f"You have {total} of records for each day")
- print('--------------------------------------------------')
- while x < number_of_sheets:
- wb.create_sheet("Sheet_" + str(x))
- x = x + 1
- for row in ws.iter_rows(min_row=1, max_col=total_cols, max_row=total):
- for cell in row:
- names = ws.cell(row=i, column=2) # this specifies which column we want to check and output
- name2 = names
- ws.cell(row=i, column=4).value = name2
Advertisement
Add Comment
Please, Sign In to add comment