I have a worksheet with two tables, after comparing and deleting duplicates from the one table I'm left with a number of empty rows in column H,I & J. I would like to move the cells up after deleting all the duplicates and have come up with this code.
>>> # Define the range of columns to check
>>> columns_to_check = ['H', 'I', 'J']
>>>
>>> # Iterate through each column
>>> for column_letter in columns_to_check:
... column_cells = worksheet[column_letter]
...
>>> # Start from the second row and iterate to the last row
>>> for row in range(2, len(column_cells) - 1):
... cell = column_cells[row]
...
>>> if cell.value is None:
File "<stdin>", line 1
if cell.value is None:
IndentationError: unexpected indent
>>> # Find the next non-empty cell below
>>> non_empty_cell_below = None
File "<stdin>", line 1
non_empty_cell_below = None
IndentationError: unexpected indent
>>> for below_row in range(row + 1, len(column_cells)):
File "<stdin>", line 1
for below_row in range(row + 1, len(column_cells)):
IndentationError: unexpected indent
>>> below_cell = column_cells[below_row]
File "<stdin>", line 1
below_cell = column_cells[below_row]
IndentationError: unexpected indent
>>> if below_cell.value:
File "<stdin>", line 1
if below_cell.value:
IndentationError: unexpected indent
>>> non_empty_cell_below = below_cell
File "<stdin>", line 1
non_empty_cell_below = below_cell
IndentationError: unexpected indent
>>> break
File "<stdin>", line 1
break
IndentationError: unexpected indent
>>>
>>> # Move the entire column up
>>> if non_empty_cell_below:
File "<stdin>", line 1
if non_empty_cell_below:
IndentationError: unexpected indent
>>> # Calculate the number of empty cells between the current cell and the non-empty cell below
>>> empty_cells_count = below_row - row - 1
File "<stdin>", line 1
empty_cells_count = below_row - row - 1
IndentationError: unexpected indent
>>>
>>> # Shift the column up by the number of empty cells
>>> for shift_row in range(row, len(column_cells) - empty_cells_count):
File "<stdin>", line 1
for shift_row in range(row, len(column_cells) - empty_cells_count):
IndentationError: unexpected indent
>>> shift_cell = column_cells[shift_row]
File "<stdin>", line 1
shift_cell = column_cells[shift_row]
IndentationError: unexpected indent
>>> below_shift_cell = column_cells[shift_row + empty_cells_count + 1]
File "<stdin>", line 1
below_shift_cell = column_cells[shift_row + empty_cells_count + 1]
IndentationError: unexpected indent
>>> shift_cell.value = below_shift_cell.value
File "<stdin>", line 1
shift_cell.value = below_shift_cell.value
IndentationError: unexpected indent
>>> below_shift_cell.value = None
File "<stdin>", line 1
below_shift_cell.value = None
IndentationError: unexpected indent
>>>
Or is there a better to move the cells up, I cant delete whole rows as there are other tables in the same rows. the table currently looks like this but the spaces can change depending on duplicates from other tables. I've attached an example spreadsheet.
# Define the range of columns to check columns_to_check = ['H', 'I', 'J'] # Iterate through each column for column_letter in columns_to_check: column_cells = destination_ws[column_letter] # Start from the second row and iterate to the last row for row in range(2, len(column_cells) - 1): cell = column_cells[row] if cell.value is None: # Find the next non-empty cell below non_empty_cell_below = None for below_row in range(row + 1, len(column_cells)): below_cell = column_cells[below_row] if below_cell.value: non_empty_cell_below = below_cell break # Move the entire column up if non_empty_cell_below: for shift_row in range(row, len(column_cells) - 1): shift_cell = column_cells[shift_row] below_shift_cell = column_cells[shift_row + 1] shift_cell.value = below_shift_cell.value below_shift_cell.value = Noneno matter how I've tried indenting it, it always have indentation errors
>>> # Define the range of columns to check
>>> columns_to_check = ['H', 'I', 'J']
>>>
>>> # Iterate through each column
>>> for column_letter in columns_to_check:
... column_cells = worksheet[column_letter]
...
>>> # Start from the second row and iterate to the last row
>>> for row in range(2, len(column_cells) - 1):
... cell = column_cells[row]
...
>>> if cell.value is None:
File "<stdin>", line 1
if cell.value is None:
IndentationError: unexpected indent
>>> # Find the next non-empty cell below
>>> non_empty_cell_below = None
File "<stdin>", line 1
non_empty_cell_below = None
IndentationError: unexpected indent
>>> for below_row in range(row + 1, len(column_cells)):
File "<stdin>", line 1
for below_row in range(row + 1, len(column_cells)):
IndentationError: unexpected indent
>>> below_cell = column_cells[below_row]
File "<stdin>", line 1
below_cell = column_cells[below_row]
IndentationError: unexpected indent
>>> if below_cell.value:
File "<stdin>", line 1
if below_cell.value:
IndentationError: unexpected indent
>>> non_empty_cell_below = below_cell
File "<stdin>", line 1
non_empty_cell_below = below_cell
IndentationError: unexpected indent
>>> break
File "<stdin>", line 1
break
IndentationError: unexpected indent
>>>
>>> # Move the entire column up
>>> if non_empty_cell_below:
File "<stdin>", line 1
if non_empty_cell_below:
IndentationError: unexpected indent
>>> # Calculate the number of empty cells between the current cell and the non-empty cell below
>>> empty_cells_count = below_row - row - 1
File "<stdin>", line 1
empty_cells_count = below_row - row - 1
IndentationError: unexpected indent
>>>
>>> # Shift the column up by the number of empty cells
>>> for shift_row in range(row, len(column_cells) - empty_cells_count):
File "<stdin>", line 1
for shift_row in range(row, len(column_cells) - empty_cells_count):
IndentationError: unexpected indent
>>> shift_cell = column_cells[shift_row]
File "<stdin>", line 1
shift_cell = column_cells[shift_row]
IndentationError: unexpected indent
>>> below_shift_cell = column_cells[shift_row + empty_cells_count + 1]
File "<stdin>", line 1
below_shift_cell = column_cells[shift_row + empty_cells_count + 1]
IndentationError: unexpected indent
>>> shift_cell.value = below_shift_cell.value
File "<stdin>", line 1
shift_cell.value = below_shift_cell.value
IndentationError: unexpected indent
>>> below_shift_cell.value = None
File "<stdin>", line 1
below_shift_cell.value = None
IndentationError: unexpected indent
>>>
Or is there a better to move the cells up, I cant delete whole rows as there are other tables in the same rows. the table currently looks like this but the spaces can change depending on duplicates from other tables. I've attached an example spreadsheet.
Attached Files