{"id":14,"date":"2026-01-24T18:24:37","date_gmt":"2026-01-24T10:24:37","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=14"},"modified":"2026-01-17T04:28:28","modified_gmt":"2026-01-17T04:28:28","slug":"how-to-delete-rows-and-columns-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-delete-rows-and-columns-using-xlwings\/","title":{"rendered":"How To Delete Rows and Columns Using xlwings?"},"content":{"rendered":"<h2>Method<\/h2>\n<h3><b>Delete empty rows<\/b><\/h3>\n<p>#xlwings API<\/p>\n<p>sht.api.Columns(&#8216;A:A&#8217;).SpecialCells(xw.constants.CellType.xlCellTypeBlanks).EntireRow.Delete()<\/p>\n<h3><b>Delete duplicate rows<\/b><\/h3>\n<p>#a=sht.cells(sht.api.Rows.Count, 1).end(&#8216;up&#8217;).row<\/p>\n<p>#for i in range(a,1,-1):<\/p>\n<p>#\u00a0\u00a0\u00a0 if app.api.WorksheetFunction.CountIf(sht.api.Columns(1), sht.api.Cells(i,1))&gt;1:<\/p>\n<p>#\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 sht.api.Rows(i).Delete()<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Delete rows\n\nimport xlwings as xw    #Import the xlwings package\n\napp=xw.App()\nbk=app.books.active    #Get the active workbook\nsht=bk.sheets.active    #Get the active worksheet\nsht.range('a1').value='abd'\nsht.range('a3').value='efg'\nsht.range('a4').value='efg'\n\n#Delete empty rows\n#xlwings API\n#sht.api.Columns('A:A').SpecialCells(xw.constants.CellType.xlCellTypeBlanks).EntireRow.Delete()\n\n#Delete duplicate rows\na=sht.cells(sht.api.Rows.Count, 1).end('up').row\nfor i in range(a,1,-1):\n    if app.api.WorksheetFunction.CountIf(sht.api.Columns(1), sht.api.Cells(i,1))>1:\n        sht.api.Rows(i).Delete()\n\n#bk.close()\n#app.kill()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Method<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-xlwings-worksheet"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":3,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":235,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions\/235"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}