{"id":2257,"date":"2026-08-20T07:37:19","date_gmt":"2026-08-19T23:37:19","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2257"},"modified":"2026-03-28T12:00:43","modified_gmt":"2026-03-28T12:00:43","slug":"how-to-use-worksheetscopy-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetscopy-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheets.Copy in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Copy<\/code> method of the <code>Worksheets<\/code> object in Excel VBA is mirrored in xlwings through the <code>api<\/code> property, which provides direct access to the underlying Excel object model. This method is used to duplicate one or more worksheets, placing the copies either before or after a specified sheet in the workbook, or into a new workbook entirely. It is particularly useful for creating templates, generating reports, or backing up data without altering the original sheets.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The method is accessed via the <code>api<\/code> property of a sheet object. The general syntax is:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><code>sheet.api.Copy(Before, After)<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>sheet<\/code><\/strong>: This is the xlwings <code>Sheet<\/code> object representing the worksheet you want to copy. You typically obtain it via <code>wb.sheets['SheetName']<\/code> or <code>wb.sheets[0]<\/code>.<\/li>\n\n\n\n<li><strong><code>Before<\/code><\/strong> (Optional, Variant): A sheet object before which the copied sheet(s) will be placed. You cannot specify both <code>Before<\/code> and <code>After<\/code>.<\/li>\n\n\n\n<li><strong><code>After<\/code><\/strong> (Optional, Variant): A sheet object after which the copied sheet(s) will be placed. You cannot specify both <code>Before<\/code> and <code>After<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Parameter Behavior:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>If neither <code>Before<\/code> nor <code>After<\/code> is specified, Excel creates a new workbook and places the copied sheet(s) there. The new workbook becomes the active workbook.<\/li>\n\n\n\n<li>You must provide either the <code>Before<\/code> or <code>After<\/code> argument to place the copy within the same workbook. These arguments are passed as Excel sheet objects, which you can get via the <code>.api<\/code> property of an xlwings <code>Sheet<\/code> object.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Examples:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Copy a sheet to a new workbook:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('MyWorkbook.xlsx')\nsource_sheet = wb.sheets&#91;'Data']\nsource_sheet.api.Copy() # Creates a new workbook with the copied sheet<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Copy a sheet and place it <em>before<\/em> a specific sheet in the same workbook:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('MyWorkbook.xlsx')\nsource_sheet = wb.sheets&#91;'Source']\ntarget_sheet = wb.sheets&#91;'Target'] # The sheet before which we place the copy\nsource_sheet.api.Copy(Before=target_sheet.api)\n# The new copy will be named \"Source (2)\" and appear before \"Target\".<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Copy a sheet and place it <em>after<\/em> the last sheet in the same workbook:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('MyWorkbook.xlsx')\nsource_sheet = wb.sheets&#91;'Report']\nall_sheets = wb.sheets\nlast_sheet = all_sheets&#91;len(all_sheets) - 1] # Get the last sheet object\nsource_sheet.api.Copy(After=last_sheet.api)\n# The copy is placed at the end of the workbook.<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Copy multiple worksheets (the entire <code>Worksheets<\/code> collection):<\/strong><br>To copy multiple sheets, you use the <code>Worksheets<\/code> collection&#8217;s <code>Copy<\/code> method. In xlwings, you can access this via the workbook&#8217;s sheets collection <code>api<\/code>.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('MyWorkbook.xlsx')\n# Select specific sheets to copy (e.g., first and third sheet)\nwb.api.Worksheets(&#91;1, 3]).Copy() # Creates a new workbook with copies of sheets 1 and 3.\n# Note: The index &#91;1, 3] uses VBA's 1-based indexing.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Copy` method of the `Worksheets` object in Excel VBA is mirrored in xlwings through the `api` p&#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-2257","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2257","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=2257"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2257\/revisions"}],"predecessor-version":[{"id":3447,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2257\/revisions\/3447"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2257"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2257"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2257"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}