{"id":2408,"date":"2026-11-03T16:16:34","date_gmt":"2026-11-03T08:16:34","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2408"},"modified":"2026-03-29T10:28:00","modified_gmt":"2026-03-29T10:28:00","slug":"how-to-use-rangeinsertindent-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangeinsertindent-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.InsertIndent in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>InsertIndent<\/code> method in Excel&#8217;s object model, accessible through the <code>xlwings<\/code> Python library, is a powerful tool for adjusting the indentation level of text within a cell or a range of cells. This functionality is essential for improving the readability and hierarchical structure of data, such as in outlines, nested lists, or financial statements where subtotals are indented. Unlike simply adding spaces, <code>InsertIndent<\/code> formally changes the cell&#8217;s formatting, ensuring consistent appearance and proper alignment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The primary purpose of the <code>InsertIndent<\/code> method is to increase the indentation of the text content. It shifts the text further from the left border of the cell by a specified number of levels. Each indent level corresponds to the standard indent increment defined in Excel (typically equivalent to three spaces or the width of one character in the default font). This method directly modifies the <code>Range.IndentLevel<\/code> property. It is important to note that <code>InsertIndent<\/code> only adds indentation; to decrease it, you would adjust the <code>IndentLevel<\/code> property directly or use other formatting methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings<\/strong><br>In <code>xlwings<\/code>, the method is called on a <code>Range<\/code> object. The syntax is straightforward:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range_object.api.InsertIndent(InsertAmount)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>range_object<\/code>: This is your <code>xlwings<\/code> Range object (e.g., <code>sheet.range('A1')<\/code> or <code>sheet.range('B2:D5')<\/code>).<\/li>\n\n\n\n<li><code>.api<\/code>: This property provides direct access to the underlying Excel object model (the COM API). The <code>InsertIndent<\/code> method is part of this native API.<\/li>\n\n\n\n<li><code>InsertAmount<\/code>: A required parameter of type <strong>Integer<\/strong>. It specifies the number of indent levels to add. The value must be greater than or equal to 0. Passing 0 has no effect.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Basic Indentation of a Single Cell:<\/strong><br>This example indents the text in cell A1 by one level.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n# Connect to the active Excel instance and workbook\napp = xw.apps.active\nwb = app.books.active\nsheet = wb.sheets&#91;'Sheet1']\n\n# Target cell A1 and insert one indent level\ntarget_cell = sheet.range('A1')\ntarget_cell.api.InsertIndent(1)<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Indenting a Contiguous Range:<\/strong><br>You can apply indentation to an entire row, column, or block of cells. This code indents all cells in the range B3 through B10 by two levels, useful for creating a sub-list.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.books.active\nsheet = wb.sheets&#91;0]\n\nlist_range = sheet.range('B3:B10')\n# Increase indentation by 2 levels for the entire range\nlist_range.api.InsertIndent(2)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Indenting Based on a Condition:<\/strong><br>This demonstrates a practical application where indentation is applied dynamically. It scans column A for cells containing the word &#8220;Subtotal&#8221; and indents those cells to visually distinguish them from main items.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.books.active\nmain_sheet = wb.sheets&#91;'Report']\n\n# Find the last used row in column A\nlast_row = main_sheet.range('A' + str(main_sheet.cells.last_cell.row)).end('up').row\n\nfor row in range(1, last_row + 1):\n    cell = main_sheet.range((row, 1)) # Column A\n    if 'Subtotal' in str(cell.value):\n        # Indent subtotal rows by one level\n        cell.api.InsertIndent(1)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `InsertIndent` method in Excel&apos;s object model, accessible through the `xlwings` Python library, &#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-2408","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2408","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=2408"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2408\/revisions"}],"predecessor-version":[{"id":3663,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2408\/revisions\/3663"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}