Search code examples
pythonexcelpandasnumpyxlwings

Is there a way to delete an entire row and shift the cells up in xlwings?


If I wanted to delete an entire row and shift the cells up is there a way to do that? Below is a snippet of my loop which is iterating through the column and clearing the contents of the cell if it doesn't match my parameters. Is there a way rather than clearing just the cell in column A I could delete the whole row and shift up?

    for i in range(lastRow):
        i = i + 1
        if sheet.range('A' + str(i)).value != 'DLQ' or 'DLR':
            xw.Range('A' + str(i)).clear()
            continue
        else:
            continue

Solution

  • Use delete() and specify the rows number(s) you want to delete in range():

    import xlwings as xw
    
    wb = xw.Book(r"test.xlsx")
    
    wb.sheets[0].range("2:2").delete()
    

    This would delete row number 2.