Python Forum
Deleting rows based on cell value in Excel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Deleting rows based on cell value in Excel
#2
This Excel file contains some names containing X or x in column 2 and some None values in column 2 or 3

Before the Excel looks like this:

Output:
>>> for row in sF[sFsheet].values: print(row) ('id', 'name', 'Age') (1, 'Peter', 1) (2, 'Xavier', 2) (3, 'George', 3) (4, None, None) (5, 'Peggy-Sue', 4) (6, 'Xander', 5) (7, 'Legolas', 6) (8, 'Happy', 7) (9, None, None) (10, 'Fatty', 8) (11, 'Sox', 9) (12, 'Justin', 10)
This deletes rows containing a name in which the letters X or x appear

def myApp():
    path2XL = '/home/pedro/myPython/openpyxl/xlsx_files/'
    file = 'some_X_or_x_in_column_2.xlsx'

    sF = openpyxl.load_workbook(path2XL + file)
    sFsheets = sF.sheetnames
    sFsheet = sFsheets[0]
    sFmaxRow = sF[sFsheet].max_row

    # open a new XL
    name = file.split('.')
    savename = name[0] + '_modified.xlsx'
    newXL = openpyxl.Workbook()
    sheets = newXL.sheetnames
    sheet = sheets[0]

    # before
    for row in sF[sFsheet].values:
        print(row)

    # you can choose the rows and columns you want to iterate
    # for row in sF[sFsheet].iter_rows(min_row=1, min_col=1, max_row=6, max_col=2):
    # look through column 2
    # if a name contains X or x delete that row
    # row[1] is the value in column 2 of the XL
    # if there is an X or x in a header name, the headers row will be gone! Fix that!
    for row in sF[sFsheet].iter_rows():
        if row[1].value == None:
            continue
        if 'X' in row[1].value or 'x' in row[1].value:
            sF[sFsheet].delete_rows(row[0].row)
        
    # after
    for row in sF[sFsheet].values:
        print(row)

    sF.save(path2XL + savename)
Reply


Messages In This Thread
RE: Deleting rows based on cell value in Excel - by Pedroski55 - Oct-10-2022, 02:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 1,588 Feb-07-2024, 12:24 PM
Last Post: Viento
  Color a table cell based on specific text Creepy 11 5,281 Jul-27-2023, 02:48 PM
Last Post: deanhystad
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 3,844 Dec-12-2022, 08:22 PM
Last Post: jh67
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 1,895 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  The code I have written removes the desired number of rows, but wrong rows Jdesi1983 0 2,141 Dec-08-2021, 04:42 AM
Last Post: Jdesi1983
  Calculate next rows based on previous values of array divon 0 2,972 Nov-23-2021, 04:44 AM
Last Post: divon
  Using Excel Cell As A Variable In A Loop knight2000 7 6,353 Aug-25-2021, 12:43 PM
Last Post: snippsat
  Using Excel Cell As A Variable In A Loop knight2000 7 8,054 Jul-18-2021, 10:52 AM
Last Post: knight2000
  Pandas DataFrame combine rows by column value, where Date Rows are NULL rhat398 0 2,707 May-04-2021, 10:51 PM
Last Post: rhat398
Smile Set 'Time' format cell when writing data to excel and not 'custom' limors 3 8,696 Mar-29-2021, 09:36 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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