{"id":2407,"date":"2026-11-03T07:08:38","date_gmt":"2026-11-02T23:08:38","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2407"},"modified":"2026-03-29T10:27:17","modified_gmt":"2026-03-29T10:27:17","slug":"how-to-use-rangeinsert-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangeinsert-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.Insert in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Insert<\/code> method of the <code>Range<\/code> object in the Excel object model is a powerful tool for dynamically modifying worksheets by shifting cells to accommodate new data or formatting. In xlwings, this functionality is exposed through the <code>api<\/code> property, allowing Python scripts to leverage Excel&#8217;s native capabilities seamlessly. This method is particularly useful for inserting cells, rows, or columns without manually moving existing content, thereby streamlining data manipulation tasks in automated workflows.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The <code>Insert<\/code> method inserts a range of cells into a worksheet, shifting other cells in the specified direction to make space. It can be applied to insert entire rows, columns, or a specific cell range. This is essential for tasks like adding new data entries, expanding tables, or reformatting layouts without disrupting existing structures.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>In xlwings, the method is accessed via the <code>api<\/code> property of a Range object. The syntax is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_obj.api.Insert(Shift, CopyOrigin)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Shift<\/strong> (optional): Specifies the direction to shift cells. It accepts values from the <code>XlInsertShiftDirection<\/code> enumeration:<\/li>\n\n\n\n<li><code>xlShiftToRight<\/code> (or <code>-4161<\/code>): Shifts cells to the right.<\/li>\n\n\n\n<li><code>xlShiftDown<\/code> (or <code>-4121<\/code>): Shifts cells downward (default if omitted).<\/li>\n\n\n\n<li><strong>CopyOrigin<\/strong> (optional): Specifies the origin for copying formatting. It uses values from the <code>XlInsertFormatOrigin<\/code> enumeration:<\/li>\n\n\n\n<li><code>xlFormatFromLeftOrAbove<\/code> (or <code>0<\/code>): Copies formatting from left or above (default).<\/li>\n\n\n\n<li><code>xlFormatFromRightOrBelow<\/code> (or <code>1<\/code>): Copies formatting from right or below.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If parameters are omitted, Excel defaults to shifting down and copying formatting from above or left.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><br>Below are practical examples demonstrating the <code>Insert<\/code> method with xlwings.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Insert a single cell and shift down:<\/strong><br>This inserts a cell at B2, moving existing cells in column B downward.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\nws.range('B2').api.Insert(Shift=-4121) # xlShiftDown\nwb.save()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Insert an entire row:<\/strong><br>Inserts a new row at row 5, shifting rows below it down. Formatting is copied from the row above.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.range('5:5').api.Insert(Shift=-4121, CopyOrigin=0)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Insert a column shifting right:<\/strong><br>Adds a new column at column C, moving existing columns to the right, with formatting from the left column.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.range('C:C').api.Insert(Shift=-4161, CopyOrigin=0) # xlShiftToRight<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Insert a range of cells:<\/strong><br>Inserts a 2&#215;2 block starting at D4, shifting cells down, and uses formatting from above or left.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.range('D4:E5').api.Insert(Shift=-4121)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Insert` method of the `Range` object in the Excel object model is a powerful tool for dynamical&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[],"class_list":["post-2407","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2407","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=2407"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2407\/revisions"}],"predecessor-version":[{"id":3661,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2407\/revisions\/3661"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}