How To Delete Rows and Columns Using xlwings?

Method

Delete empty rows

#xlwings API

sht.api.Columns(‘A:A’).SpecialCells(xw.constants.CellType.xlCellTypeBlanks).EntireRow.Delete()

Delete duplicate rows

#a=sht.cells(sht.api.Rows.Count, 1).end(‘up’).row

#for i in range(a,1,-1):

#    if app.api.WorksheetFunction.CountIf(sht.api.Columns(1), sht.api.Cells(i,1))>1:

#        sht.api.Rows(i).Delete()

Sample Code

#Delete rows

import xlwings as xw    #Import the xlwings package

app=xw.App()
bk=app.books.active    #Get the active workbook
sht=bk.sheets.active    #Get the active worksheet
sht.range('a1').value='abd'
sht.range('a3').value='efg'
sht.range('a4').value='efg'

#Delete empty rows
#xlwings API
#sht.api.Columns('A:A').SpecialCells(xw.constants.CellType.xlCellTypeBlanks).EntireRow.Delete()

#Delete duplicate rows
a=sht.cells(sht.api.Rows.Count, 1).end('up').row
for i in range(a,1,-1):
    if app.api.WorksheetFunction.CountIf(sht.api.Columns(1), sht.api.Cells(i,1))>1:
        sht.api.Rows(i).Delete()

#bk.close()
#app.kill()
January 24, 2026 (0)


Leave a Reply

Your email address will not be published. Required fields are marked *