{"id":13,"date":"2026-01-24T11:55:31","date_gmt":"2026-01-24T03:55:31","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=13"},"modified":"2026-01-17T04:27:15","modified_gmt":"2026-01-17T04:27:15","slug":"how-to-insert-rows-and-columns-using-xlwings","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-insert-rows-and-columns-using-xlwings\/","title":{"rendered":"How To Insert Rows and Columns Using xlwings?"},"content":{"rendered":"<h2>Method<\/h2>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Row<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">sht.range(&#8216;A2&#8217;).color=(0,255,0)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.range(&#8216;C2&#8217;).color=(0,0,255)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.range(&#8216;E2&#8217;).color=(255,0,0)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht[&#8216;3:3&#8242;].insert(shift=&#8217;down&#8217;,copy_origin=&#8217;format_from_left_or_above&#8217;)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Range(&#8216;A2&#8217;).Interior.Color=xw.utils.rgb_to_int((0, 255, 0))<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Range(&#8216;C2&#8217;).Interior.Color=xw.utils.rgb_to_int((0, 0, 255))<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Range(&#8216;E2&#8217;).Interior.Color=xw.utils.rgb_to_int((255, 0, 0))<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Rows(3).Insert(Shift=xw.constants.InsertShiftDirection.xlShiftDown,CopyOrigin=xw.constants.InsertFormatOrigin.xlFormatFromLeftOrAbove)<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 10.0pt;\">\u00a0<\/p>\n<h3 style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\"><span style=\"font-weight: bold;\">Column<\/span><\/h3>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht[&#8216;B:B&#8217;].insert()<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 8.0pt;\">\u00a0<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#xlwings API<\/p>\n<p style=\"margin: 0in; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">#sht.api.Columns(2).Insert()<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code># Insert rows and columns\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\n\n#Row\n#xlwings\nsht.range('A2').color=(0,255,0)\nsht.range('C2').color=(0,0,255)\nsht.range('E2').color=(255,0,0)\nsht&#91;'3:3'].insert(shift='down',copy_origin='format_from_left_or_above')\n\n#xlwings API\n#sht.api.Range('A2').Interior.Color=xw.utils.rgb_to_int((0, 255, 0))\n#sht.api.Range('C2').Interior.Color=xw.utils.rgb_to_int((0, 0, 255))\n#sht.api.Range('E2').Interior.Color=xw.utils.rgb_to_int((255, 0, 0))\n#sht.api.Rows(3).Insert(Shift=xw.constants.InsertShiftDirection.xlShiftDown,CopyOrigin=xw.constants.InsertFormatOrigin.xlFormatFromLeftOrAbove)\n\n#Column\n#xlwings\n#sht&#91;'B:B'].insert()\n\n#xlwings API\n#sht.api.Columns(2).Insert()\n\n#bk.close()\n#app.kill()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\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-13","post","type-post","status-publish","format-standard","hentry","category-xlwings-worksheet"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/13","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=13"}],"version-history":[{"count":3,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":233,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/233"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}