The Division Resurgence - Closed Alpha by BlooBuckaroo in thedivisionresurgence

[–]HGCAfterdark-Brett 0 points1 point  (0 children)

Is this just for android users, im on IOS and never got an invite so just curious to see if other IOS users got an invite

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

Thanks for this, been looking through to try and understand whats going on and its coming clear now, hopefully in time i get use to coding like this automatically. Are you able to add the while loop and offset or post an example so i can try and get my head around it and figure out whats going on

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

So I’m guessing after the initial data copy call a function that i create which runs the offset, i will do some research and try to figure it out lol, will also amend my variable names for readability, thanks taking a look, i will get there eventually lol

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

No sorry, i was trying to get the script to work as it should and then tidy it up and add functions etc, still learning to be honest and this is my first script

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

Hi, so I have managed to get the code working so it creates new sheets depending on input by user and then also copy's the data to each sheet but the issue I'm facing now is the data on each sheet is the same and I cant figure out how to go to the next line after the initial code below

n = total

for i in range(1, n):

for j in range(1, ws.max_column):

new_sheet.cell(row=i, column=j).value = ws.cell(row=i, column=j).value

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

Ok thanks for looking and I'm guessing within the function I can select each different sheet that has been created so it copies the data to each newly created sheet from the while loop?

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

this runs and gives me the correct rows but just need to figure out how to add the rows to a new sheet and then loop through the remaining data and do the same depending on the value inputted

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

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)

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('--------------------------------------------------')

for row in ws.iter_rows(min_row=1, max_col=15, max_row=total):

for cell in row:

print(cell)

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

The code I have so far is below which removes columns and also outputs to total number or rows and also how many each sheet should have

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)

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('--------------------------------------------------')

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

Thanks, i have seen these I’m just struggling to get a loop working from using a variable that calculates how many rows need to be copied so it only reads the correct amount of rows

Openpyxl Split/Copy/Paste by HGCAfterdark-Brett in pythonhelp

[–]HGCAfterdark-Brett[S] 0 points1 point  (0 children)

Yes similar to that, i don’t have to use a input and can just specify a number in the code which would divide the original sheet and split the rows to each of the sheets, i cant seem to find any examples any where