{"id":11,"date":"2026-01-23T11:56:56","date_gmt":"2026-01-23T03:56:56","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=11"},"modified":"2026-01-16T05:56:37","modified_gmt":"2026-01-16T05:56:37","slug":"how-to-insert-and-delete-rowscolumns-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-insert-and-delete-rowscolumns-using-openpyxl.html","title":{"rendered":"How To Insert and Delete Rows\/Columns Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">To insert rows or columns, use the `insert_rows` and `insert_cols` methods.<\/p>\n<p lang=\"en-US\" style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws.insert_rows(idx,amount=1)<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; ws.insert_rows(idx=2,amount=5)<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws.insert_cols(idx,amount=1)<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt; color: black;\">&gt;&gt;&gt; ws.insert_cols(idx=2,amount=2)<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; line-height: 10pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt; color: black;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">To delete rows or columns, use `delete_rows` and `delete_cols` methods.<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws.delete_cols(idx,amount=1)<\/p>\n<p lang=\"en-US\" style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">ws.delete_rows(idx,amount=1)<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Inserting and Deleting Rows and Columns\n\n#Import load_workbook function\nfrom openpyxl import Workbook\n\n#Create a new workbook\nwb=Workbook()\n#Get the active worksheet\nws=wb.active\n\n#Insert 1 empty row above the 5th row\nws.insert_rows(5)\n#Insert 3 empty rows above the 5th row\nws.insert_rows(5,3)\n\n#Insert 1 empty column to the left of the 4th column\nws.insert_cols(4)\n#Insert 3 empty columns to the left of the 4th column\nws.insert_cols(4,3)\n\n#Delete the 5th row\nws.delete_rows(5)\n#Delete the 4th column\nws.delete_cols(4)\n\n#Delete 3 rows starting from the 5th row\n#(including the 5th row)\nws.delete_rows(5,3)\n#Delete 3 columns starting from the 4th column\n#(including the 4th column)\nws.delete_cols(4,3)\n\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>OpenPyXL<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-11","post","type-post","status-publish","format-standard","hentry","category-openpyxl-worksheet"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/11","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":214,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/11\/revisions\/214"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}