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
#5
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
Reply


Messages In This Thread
RE: getting unexpected indent errors trying to move cells up - by deanhystad - Jun-28-2023, 12:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  xml indent SubElements (wrapping) with multiple strings ctrldan 2 1,677 Jun-09-2023, 08:42 PM
Last Post: ctrldan
  IndentationError: unexpected indent dee 3 2,488 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 10,208 Mar-30-2022, 11:05 PM
Last Post: Pedroski55
  Avoid multiple repeat in indent Frankduc 8 3,001 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,633 Feb-11-2021, 09:31 PM
Last Post: nilamo
  Copy certain cells into new workbook certain cells Kristenl2784 4 2,566 Jul-14-2020, 07:59 PM
Last Post: Kristenl2784
  IndentationError: unexpected indent jk91 1 2,447 Feb-27-2020, 08:56 PM
Last Post: buran
  could not fix unexpected indent Bayan 1 3,310 Nov-08-2019, 01:45 PM
Last Post: ichabod801
  unable to indent json data dataplumber 4 3,131 Oct-22-2019, 01:55 PM
Last Post: dataplumber
  beginner : unexpected EOF errors upasana 6 4,853 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