{"id":2283,"date":"2026-09-02T07:57:38","date_gmt":"2026-09-01T23:57:38","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2283"},"modified":"2026-03-28T12:19:03","modified_gmt":"2026-03-28T12:19:03","slug":"how-to-use-worksheetmove-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetmove-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Move in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>Move<\/strong> member of the <strong>Worksheet<\/strong> object in the Excel object model, accessible via the xlwings library in Python, provides a programmatic way to reposition a worksheet within its workbook. This functionality is essential for organizing workbook structure, such as reordering sheets for logical presentation or moving a newly created sheet to a specific location. Unlike simply activating a sheet, the Move method physically changes the sheet&#8217;s index in the workbook&#8217;s tab order.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In xlwings, the method is accessed through a <code>Sheet<\/code> object (which represents a Worksheet). The core syntax for its API call is:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sheet.move(before, after)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Both parameters, <code>before<\/code> and <code>after<\/code>, are optional but mutually exclusive\u2014you should specify only one. They accept either an xlwings <code>Sheet<\/code> object or an integer index.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>before<\/code><\/strong>: The sheet before which the current sheet will be placed. If specified as a <code>Sheet<\/code> object, the moved sheet will be positioned immediately before that specific sheet. If specified as an integer (1-based index), the moved sheet will be moved to a position before the sheet currently at that index.<\/li>\n\n\n\n<li><strong><code>after<\/code><\/strong>: The sheet after which the current sheet will be placed. If specified as a <code>Sheet<\/code> object, the moved sheet will be positioned immediately after that specific sheet. If specified as an integer (1-based index), the moved sheet will be moved to a position after the sheet currently at that index.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">If neither <code>before<\/code> nor <code>after<\/code> is specified, Excel will create a new workbook containing only the moved worksheet. The following table summarizes the behavior:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th class=\"has-text-align-left\" data-align=\"left\">Parameter Provided<\/th><th class=\"has-text-align-left\" data-align=\"left\">Resulting Action<\/th><\/tr><\/thead><tbody><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>before=target<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">Moves the sheet to a position immediately <strong>before<\/strong> the <code>target<\/code> sheet.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\"><code>after=target<\/code><\/td><td class=\"has-text-align-left\" data-align=\"left\">Moves the sheet to a position immediately <strong>after<\/strong> the <code>target<\/code> sheet.<\/td><\/tr><tr><td class=\"has-text-align-left\" data-align=\"left\">Neither parameter<\/td><td class=\"has-text-align-left\" data-align=\"left\">Moves the sheet to a <strong>new, single-sheet workbook<\/strong>.<\/td><\/tr><\/tbody><\/table><\/figure>\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>Move a sheet to the beginning of the workbook (before the first sheet):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book(\"report.xlsx\")\nsheet_to_move = wb.sheets&#91;\"DataSheet\"]\nfirst_sheet = wb.sheets&#91;0] # Index 0 refers to the first sheet\nsheet_to_move.move(before=first_sheet)<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Move a sheet to the end of the workbook (after the last sheet):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book()\nsummary_sheet = wb.sheets.add(\"Summary\")\nlast_sheet = wb.sheets&#91;-1] # Index -1 refers to the last sheet\nsummary_sheet.move(after=last_sheet)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Move a sheet to a specific index position (e.g., to become the third sheet):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book()\nanalysis_sheet = wb.sheets&#91;\"Analysis\"]\n# To place it as the third sheet, move it before the current third sheet.\n# Index is 1-based in the `move` method context for this operation.\nanalysis_sheet.move(before=3)<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Move a sheet relative to another named sheet:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book(\"dashboard.xlsx\")\nchart_sheet = wb.sheets&#91;\"Charts\"]\npivot_sheet = wb.sheets&#91;\"PivotTables\"]\n# Place the Charts sheet right after the PivotTables sheet\nchart_sheet.move(after=pivot_sheet)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **Move** member of the **Worksheet** object in the Excel object model, accessible via the xlwing&#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-2283","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2283","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=2283"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2283\/revisions"}],"predecessor-version":[{"id":3484,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2283\/revisions\/3484"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}