Jun-28-2023, 12:05 PM
(This post was last modified: Jun-28-2023, 12:05 PM by deanhystad.)
Sounds like you want to move all the empty cells to the end. I would do this by collecting all the non-empty cell values and padding the list to the column length with None. Then you can copy the filtered values back to the column. Like this:
from openpyxl import load_workbook wb = load_workbook("test.xlsx") sheet = wb.worksheets[0] for column in sheet.columns: values = [cell.value for cell in column if cell.value is not None] # Get values for all the non-empty cells values = values + [None] * (len(column) - len(values)) # Pad with None to the column length. for cell, value in zip(column, values): # Copy the values to the column. cell.value = value