Python Forum
getting unexpected indent errors trying to move cells up
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
getting unexpected indent errors trying to move cells up
#1
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 = 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 = None
no 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

.xlsx   example workbook2.xlsx (Size: 9.42 KB / Downloads: 79)
Reply


Messages In This Thread
getting unexpected indent errors trying to move cells up - by jensengt - Jun-27-2023, 02:21 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,563 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  IndentationError: unexpected indent dee 3 2,393 May-02-2022, 02:15 AM
Last Post: dee
  Openpyxl-change value of cells in column based on value that currently occupies cells phillipaj1391 5 9,979 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  Avoid multiple repeat in indent Frankduc 8 2,948 Jan-18-2022, 05:46 PM
Last Post: Frankduc
  How can I iterate through all cells in a column (with merge cells) with openpyxl? aquerci 1 7,567 Feb-11-2021, 09:31 PM
Last Post: nilamo
  Copy certain cells into new workbook certain cells Kristenl2784 4 2,523 Jul-14-2020, 07:59 PM
Last Post: Kristenl2784
  IndentationError: unexpected indent jk91 1 2,410 Feb-27-2020, 08:56 PM
Last Post: buran
  could not fix unexpected indent Bayan 1 3,241 Nov-08-2019, 01:45 PM
Last Post: ichabod801
  unable to indent json data dataplumber 4 3,068 Oct-22-2019, 01:55 PM
Last Post: dataplumber
  beginner : unexpected EOF errors upasana 6 4,749 Mar-19-2018, 04:18 PM
Last Post: upasana

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020