{"id":2330,"date":"2026-09-25T15:09:30","date_gmt":"2026-09-25T07:09:30","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2330"},"modified":"2026-03-28T13:14:20","modified_gmt":"2026-03-28T13:14:20","slug":"how-to-use-worksheetname-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetname-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Name in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Name<\/code> property of a <code>Worksheet<\/code> object in the Excel object model is a fundamental attribute used to get or set the name of a worksheet. In xlwings, this property is accessed directly through the <code>Worksheet<\/code> object, allowing for straightforward retrieval and modification of sheet names within a workbook. This capability is essential for tasks such as dynamically referencing sheets, organizing data across multiple sheets, or automating sheet management processes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary function is to read or change the name of a worksheet. This is useful in scenarios where sheet names need to be updated based on data content, user input, or automated workflows. For example, you might rename sheets after importing data to reflect the dataset&#8217;s source or date.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>In xlwings, the <code>Name<\/code> property is accessed as an attribute of a <code>Worksheet<\/code> object. The syntax is simple:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>To get the current name: <code>sheet_name = ws.name<\/code><\/li>\n\n\n\n<li>To set a new name: <code>ws.name = \"NewSheetName\"<\/code><br>Here, <code>ws<\/code> represents a <code>Worksheet<\/code> object obtained via <code>wb.sheets['Sheet1']<\/code> or similar. The name must be a string and adhere to Excel&#8217;s naming conventions (e.g., no more than 31 characters, and characters like <code>:<\/code>, <code>\\<\/code>, <code>\/<\/code>, <code>?<\/code>, <code>*<\/code>, <code>[<\/code>, <code>]<\/code> are not allowed). If an invalid name is provided, xlwings will raise an error.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><br>Below are practical examples demonstrating the use of the <code>Name<\/code> property with xlwings.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Retrieving a Worksheet Name:<\/strong><br>This example opens an existing workbook and prints the name of the first worksheet.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n# Open an existing workbook\nwb = xw.Book('example.xlsx')\n# Access the first worksheet\nws = wb.sheets&#91;0]\n# Get and print the worksheet name\ncurrent_name = ws.name\nprint(f\"The worksheet name is: {current_name}\")<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Renaming a Worksheet:<\/strong><br>This example renames a specific worksheet to &#8220;DataSummary&#8221;.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n# Open an existing workbook\nwb = xw.Book('example.xlsx')\n# Access a worksheet by its current name\nws = wb.sheets&#91;'Sheet1']\n# Change the worksheet name\nws.name = \"DataSummary\"\n# Save the workbook to persist changes\nwb.save()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Dynamic Renaming Based on Content:<\/strong><br>This example renames all worksheets in a workbook based on a list of new names, demonstrating batch processing.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n# Open an existing workbook\nwb = xw.Book('data.xlsx')\n# Define new names for each worksheet\nnew_names = &#91;\"January\", \"February\", \"March\"]\n# Iterate through worksheets and rename them\nfor i, ws in enumerate(wb.sheets):\n    if i &lt; len(new_names):\n        ws.name = new_names&#91;i]\n# Save the workbook\nwb.save()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Name` property of a `Worksheet` object in the Excel object model is a fundamental attribute use&#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-2330","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2330","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=2330"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2330\/revisions"}],"predecessor-version":[{"id":3550,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2330\/revisions\/3550"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2330"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2330"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2330"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}