{"id":2419,"date":"2026-11-09T07:46:21","date_gmt":"2026-11-08T23:46:21","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2419"},"modified":"2026-03-29T10:34:34","modified_gmt":"2026-03-29T10:34:34","slug":"how-to-use-rangeremovesubtotal-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-rangeremovesubtotal-in-the-xlwings-api-way\/","title":{"rendered":"How to use Range.RemoveSubtotal in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>RemoveSubtotal<\/code> method in the Excel object model is a member of the <code>Range<\/code> object. It is used to remove subtotals that have been previously applied to a list or range of data within an Excel worksheet. Subtotals are typically created using Excel&#8217;s built-in Subtotal feature, which groups data and inserts summary rows with functions like SUM, COUNT, or AVERAGE. The <code>RemoveSubtotal<\/code> method clears these groupings and summary rows, restoring the range to its original state without the subtotal calculations and outline structure. This is particularly useful when cleaning up data or preparing it for different analysis after subtotals are no longer needed.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, the <code>RemoveSubtotal<\/code> method can be accessed through the <code>api<\/code> property of a <code>Range<\/code> object, which provides direct access to the underlying Excel object model. The syntax for calling this method in xlwings is straightforward, as it does not require any parameters. The method is called on a <code>Range<\/code> object that represents the entire range where subtotals are applied, and it removes all subtotals within that range.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The xlwings API call format is:<br><code>range.api.RemoveSubtotal()<\/code><br>Here, <code>range<\/code> is an xlwings <code>Range<\/code> object (e.g., <code>sheet.range('A1:D100')<\/code>). The method does not take any arguments, and it does not return a value. It directly modifies the worksheet by removing subtotals. It is important to note that if no subtotals exist in the specified range, calling this method will have no effect and will not raise an error.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, consider an Excel worksheet where subtotals have been applied to a dataset in the range A1:D50 to summarize sales data by region. To remove these subtotals using xlwings, you would first connect to the workbook and worksheet, then call the <code>RemoveSubtotal<\/code> method on the appropriate range. Below is a code instance demonstrating this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active workbook and sheet (adjust as needed)\nwb = xw.books.active\nsheet = wb.sheets&#91;'Sheet1']\n\n# Define the range where subtotals are applied\nsubtotal_range = sheet.range('A1:D50')\n\n# Remove subtotals from the range\nsubtotal_range.api.RemoveSubtotal()\n\n# Save the workbook if changes are to be persisted\nwb.save()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `RemoveSubtotal` method in the Excel object model is a member of the `Range` object. It is used &#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-2419","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2419","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=2419"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2419\/revisions"}],"predecessor-version":[{"id":3675,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2419\/revisions\/3675"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2419"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2419"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2419"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}