{"id":2406,"date":"2026-11-02T16:30:01","date_gmt":"2026-11-02T08:30:01","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2406"},"modified":"2026-03-29T10:26:52","modified_gmt":"2026-03-29T10:26:52","slug":"how-to-use-rangegroup-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangegroup-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.Group in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Group<\/code> member of the <code>Range<\/code> object in the Excel object model is a method used to group rows or columns in an outline, which is particularly useful for creating structured, collapsible sections in worksheets for summarizing data. In xlwings, this functionality is accessible through the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This method allows you to manage grouped ranges programmatically, enhancing automation in report generation and data presentation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary purpose of the <code>Group<\/code> method is to group selected rows or columns, enabling users to collapse or expand them for better data organization. It is commonly applied in financial models, detailed lists, or any dataset where hierarchical viewing is beneficial. Grouping helps in hiding detail while showing summary rows or columns, making worksheets more readable.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, the syntax for using the <code>Group<\/code> method is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>range.api.Group(Start, End, ByRows, Periods)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Start<\/code> (Optional, Variant): Specifies the first row or column to group. If omitted, grouping starts from the first cell in the range.<\/li>\n\n\n\n<li><code>End<\/code> (Optional, Variant): Specifies the last row or column to group. If omitted, grouping ends at the last cell in the range.<\/li>\n\n\n\n<li><code>ByRows<\/code> (Optional, Boolean): Determines whether to group by rows (<code>True<\/code>) or columns (<code>False<\/code>). The default is <code>True<\/code> if not specified.<\/li>\n\n\n\n<li><code>Periods<\/code> (Optional, Integer): Indicates the number of periods to group, typically used for creating multiple outline levels. The default is 1.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">A table summarizing parameter details:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Parameter<\/th><th>Type<\/th><th>Description<\/th><th>Default Value<\/th><\/tr><\/thead><tbody><tr><td>Start<\/td><td>Variant<\/td><td>First row\/column index<\/td><td>First cell in range<\/td><\/tr><tr><td>End<\/td><td>Variant<\/td><td>Last row\/column index<\/td><td>Last cell in range<\/td><\/tr><tr><td>ByRows<\/td><td>Boolean<\/td><td>Group by rows (True) or columns (False)<\/td><td>True<\/td><\/tr><tr><td>Periods<\/td><td>Integer<\/td><td>Number of outline levels<\/td><td>1<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><br>Here is an xlwings code example demonstrating the use of the <code>Group<\/code> method to group rows in a worksheet:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active workbook\nwb = xw.books.active\nws = wb.sheets&#91;'Sheet1']\n\n# Define a range, e.g., rows 5 to 10\ntarget_range = ws.range('5:10')\n\n# Group the rows in the range\ntarget_range.api.Group(Start=None, End=None, ByRows=True, Periods=1)\n\n# To group columns, for instance columns B to D\ncol_range = ws.range('B:D')\ncol_range.api.Group(Start=None, End=None, ByRows=False, Periods=1)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Group` member of the `Range` object in the Excel object model is a method used to group rows or&#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-2406","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2406","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=2406"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2406\/revisions"}],"predecessor-version":[{"id":3660,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2406\/revisions\/3660"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2406"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2406"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2406"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}