{"id":2149,"date":"2026-06-27T07:17:38","date_gmt":"2026-06-26T23:17:38","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2149"},"modified":"2026-03-28T08:50:57","modified_gmt":"2026-03-28T08:50:57","slug":"how-to-use-applicationnames-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationnames-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.Names in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Names<\/code> member of the <code>Application<\/code> object in Excel&#8217;s object model is a powerful collection that represents all the defined names within a workbook or application scope. In xlwings, this is accessed through the <code>app.names<\/code> property. Defined names are essentially named ranges or constants that make formulas more readable and dynamic. They can refer to a single cell, a range of cells, a constant value, or even a formula. Using the <code>Names<\/code> collection via xlwings allows you to programmatically create, modify, retrieve, and delete these names, which is crucial for building robust, maintainable Excel-based automation and data models.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The primary syntax in xlwings for interacting with this collection is through the <code>app.names<\/code> property, which returns a <code>Names<\/code> collection object. You can also access it via a specific workbook: <code>wb.names<\/code>. Key methods and properties include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>add(name, refers_to)<\/code>: Creates a new defined name. <code>name<\/code> is a string for the name (cannot contain spaces and must begin with a letter or underscore). <code>refers_to<\/code> is a string defining the reference, using standard Excel notation (e.g., <code>\"=Sheet1!$A$1:$D$10\"<\/code> or <code>\"=5\"<\/code> for a constant).<\/li>\n\n\n\n<li><code>item(index_or_name)<\/code>: Returns a specific <code>Name<\/code> object, either by its string name or its numerical index in the collection.<\/li>\n\n\n\n<li><code>count<\/code>: Returns the number of defined names in the collection.<\/li>\n\n\n\n<li>On a <code>Name<\/code> object, key properties are <code>name<\/code> (to get or set the name text) and <code>refers_to<\/code> (to get or set the reference formula string). The <code>delete()<\/code> method removes the name.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Here are practical xlwings code examples demonstrating the use of the <code>Application.Names<\/code> member:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel instance and its active workbook\napp = xw.apps.active\nwb = app.books.active\n\n# Example 1: Adding a new defined name for a range\n# This creates a name \"DataRange\" referring to cells A1:D20 on Sheet1\nwb.names.add(name=\"DataRange\", refers_to=\"=Sheet1!$A$1:$D$20\")\n\n# Example 2: Adding a named constant\n# This creates a name \"TaxRate\" with a constant value of 0.075\napp.names.add(name=\"TaxRate\", refers_to=\"=0.075\")\n\n# Example 3: Retrieving and inspecting a defined name\n# Get a specific name object and print its details\ntry:\n    data_name = wb.names&#91;\"DataRange\"]\n    print(f\"Name: {data_name.name}\")\n    print(f\"Refers to: {data_name.refers_to}\")\nexcept KeyError:\n    print(\"Name not found.\")\n\n# Example 4: Iterating through all defined names in the workbook\nprint(f\"\\nTotal names in workbook: {wb.names.count}\")\nfor name_obj in wb.names:\n    print(f\" - {name_obj.name}: {name_obj.refers_to}\")\n\n# Example 5: Modifying an existing name's reference\n# Change the \"DataRange\" to refer to a dynamic range using the OFFSET function\ndata_name = wb.names&#91;\"DataRange\"]\ndata_name.refers_to = \"=OFFSET(Sheet1!$A$1,0,0,COUNTA(Sheet1!$A:$A),4)\"\n\n# Example 6: Deleting a defined name\napp.names&#91;\"TaxRate\"].delete()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Names` member of the `Application` object in Excel&apos;s object model is a powerful collection that&#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-2149","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2149","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=2149"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2149\/revisions"}],"predecessor-version":[{"id":3285,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2149\/revisions\/3285"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}