{"id":2228,"date":"2026-08-05T16:29:06","date_gmt":"2026-08-05T08:29:06","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2228"},"modified":"2026-03-28T11:36:33","modified_gmt":"2026-03-28T11:36:33","slug":"how-to-use-applicationworksheets-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationworksheets-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.Worksheets in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In the Excel object model, the <strong>Worksheets<\/strong> collection is a crucial component under the <strong>Application<\/strong> object, representing all worksheets within a workbook. Through xlwings, a powerful Python library for Excel automation, developers can programmatically access and manipulate these worksheets, enabling dynamic data analysis, reporting, and visualization. The <strong>Worksheets<\/strong> member provides methods to add, delete, reference, and iterate over sheets, making it essential for tasks like batch processing or creating dashboards.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong>: The <strong>Worksheets<\/strong> collection allows you to manage worksheets in an Excel workbook. You can retrieve a specific sheet by name or index, add new sheets, count the total number of sheets, and perform operations across multiple sheets. This is particularly useful for automating repetitive tasks, such as consolidating data from multiple sources or generating standardized reports.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong>: In xlwings, the <strong>Worksheets<\/strong> collection is accessed via the <code>api<\/code> property of a workbook or application object. The general syntax is:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>app.worksheets<\/code>: Returns a collection of all worksheets in the active workbook.<\/li>\n\n\n\n<li><code>app.worksheets[index]<\/code>: Accesses a worksheet by its index (1-based).<\/li>\n\n\n\n<li><code>app.worksheets[\"SheetName\"]<\/code>: Accesses a worksheet by its name.<\/li>\n\n\n\n<li><code>app.worksheets.add()<\/code>: Adds a new worksheet to the workbook.<\/li>\n\n\n\n<li><code>app.worksheets.count<\/code>: Returns the number of worksheets.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Parameters for methods like <code>add()<\/code> include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>before<\/code>: Specifies the sheet before which the new sheet is added (can be a sheet object or index).<\/li>\n\n\n\n<li><code>after<\/code>: Specifies the sheet after which the new sheet is added.<\/li>\n\n\n\n<li><code>count<\/code>: The number of sheets to add (default is 1).<br>If not specified, the new sheet is added after all existing sheets.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example Code<\/strong>: Below are practical xlwings API examples demonstrating the use of the <strong>Worksheets<\/strong> member:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Accessing Worksheets by Index and Name<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=False) # Start Excel in background\nwb = app.books.open(\"example.xlsx\")\n# Access first worksheet\nws1 = wb.worksheets&#91;0]\n# Access worksheet by name\nws2 = wb.worksheets&#91;\"DataSheet\"]\nprint(f\"First sheet name: {ws1.name}, DataSheet range A1: {ws2.range('A1').value}\")\nwb.close()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Adding and Counting Worksheets<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=True)\nwb = app.books.add()\n# Add a new sheet after the first one\nnew_sheet = wb.worksheets.add(after=wb.worksheets&#91;0])\nnew_sheet.name = \"Analysis\"\n# Count total worksheets\nsheet_count = wb.worksheets.count\nprint(f\"Total sheets: {sheet_count}\")\n# Save and close\nwb.save(\"new_workbook.xlsx\")\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Iterating Over All Worksheets<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=False)\nwb = app.books.open(\"data.xlsx\")\n# Loop through each worksheet and print names\nfor ws in wb.worksheets:\n    print(f\"Processing sheet: {ws.name}\")\n    # Example: Clear content from column A\nws.range(\"A:A\").clear()\nwb.save()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Deleting a Worksheet<\/strong>:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=False)\nwb = app.books.open(\"report.xlsx\")\n# Delete a sheet by name\nif \"TempSheet\" in &#91;sheet.name for sheet in wb.worksheets]:\n    wb.worksheets&#91;\"TempSheet\"].delete()\nwb.save()\napp.quit()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In the Excel object model, the **Worksheets** collection is a crucial component under the **Applicat&#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-2228","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2228","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=2228"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2228\/revisions"}],"predecessor-version":[{"id":3406,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2228\/revisions\/3406"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}