{"id":2255,"date":"2026-08-19T07:25:27","date_gmt":"2026-08-18T23:25:27","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2255"},"modified":"2026-03-28T11:59:41","modified_gmt":"2026-03-28T11:59:41","slug":"how-to-use-worksheetsadd-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetsadd-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheets.Add in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>Add<\/strong> member of the <strong>Worksheets<\/strong> object in the Excel object model is a method used to create a new worksheet. In xlwings, this functionality is accessed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model (via pywin32 on Windows or appscript on macOS). This allows you to programmatically add sheets to a workbook, offering control over the sheet&#8217;s position and name.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The primary purpose of the <code>Add<\/code> method is to insert a new worksheet into a workbook. You can specify where the new sheet should be placed relative to existing sheets and what its name should be. This is essential for automating report generation, data organization, or creating dynamic dashboards where the number of sheets may vary based on the data.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings<\/strong><br>The general syntax using xlwings is:<br><code>workbook.api.Worksheets.Add(Before, After, Count, Type)<\/code><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The parameters are:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Before<\/strong> (Optional, Variant): A worksheet object that specifies the sheet before which the new sheet will be added. You cannot use both <code>Before<\/code> and <code>After<\/code>.<\/li>\n\n\n\n<li><strong>After<\/strong> (Optional, Variant): A worksheet object that specifies the sheet after which the new sheet will be added. You cannot use both <code>Before<\/code> and <code>After<\/code>.<\/li>\n\n\n\n<li><strong>Count<\/strong> (Optional, Variant): The number of new worksheets to add. The default value is 1.<\/li>\n\n\n\n<li><strong>Type<\/strong> (Optional, Variant): The type of sheet to add. Can be <code>xlWorksheet<\/code> (value -4167) for a standard worksheet or <code>xlChart<\/code> (value -4109) for a chart sheet. The default is <code>xlWorksheet<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">To use a parameter, you typically pass a worksheet object (e.g., <code>wb.sheets['Sheet1'].api<\/code>) for <code>Before<\/code> or <code>After<\/code>, or an integer for <code>Count<\/code>. If both <code>Before<\/code> and <code>After<\/code> are omitted, the new sheet is added before the active sheet.<\/p>\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>Add a single worksheet with a default name (e.g., &#8220;Sheet4&#8221;):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book() # Opens a new workbook\nnew_sheet = wb.api.Worksheets.Add()\n# The new worksheet object is now in 'new_sheet'<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Add a worksheet after a specific sheet and rename it:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('Report.xlsx')\n# Add new sheet after the sheet named \"Data\"\nnew_sheet = wb.api.Worksheets.Add(After=wb.sheets&#91;'Data'].api)\nnew_sheet.Name = \"Summary\" # Rename the new sheet<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Add multiple worksheets at the beginning of the workbook:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book()\nfirst_sheet = wb.sheets&#91;0].api # Get the API object of the first sheet\n# Add 3 new sheets before the first sheet\nwb.api.Worksheets.Add(Before=first_sheet, Count=3)<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Add a chart sheet at the end of the workbook:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nfrom xlwings.constants import ChartType\nwb = xw.Book()\nlast_sheet = wb.sheets&#91;-1].api # Get the API object of the last sheet\n# Add a chart sheet after the last worksheet\nchart_sheet = wb.api.Worksheets.Add(After=last_sheet, Type=ChartType.xlChart)\n# Note: Chart sheets are a different object type than worksheets in the Excel model.<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **Add** member of the **Worksheets** object in the Excel object model is a method used to create&#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-2255","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2255","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=2255"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2255\/revisions"}],"predecessor-version":[{"id":3444,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2255\/revisions\/3444"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2255"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2255"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2255"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}