{"id":1990,"date":"2026-04-08T15:00:50","date_gmt":"2026-04-08T07:00:50","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=1990"},"modified":"2026-03-28T04:07:05","modified_gmt":"2026-03-28T04:07:05","slug":"how-to-use-applicationgetcustomlistcontents-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-applicationgetcustomlistcontents-in-the-xlwings-api-way\/","title":{"rendered":"How to use Application.GetCustomListContents in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>GetCustomListContents<\/code> member of the <code>Application<\/code> object in Excel is a method that retrieves the contents of a custom list. Custom lists are used for custom sorting or filling series, such as a list of department names, weekdays, or months in a specific language order. In xlwings, this method allows Python scripts to access these lists programmatically, enabling dynamic data processing and automation based on user-defined sequences. This is particularly useful for applications that require consistent sorting or pattern generation across different Excel workbooks or when integrating Excel data with other systems.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, the <code>GetCustomListContents<\/code> method is accessed through the <code>app<\/code> object, which represents the Excel application. The syntax is as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>contents = app.api.GetCustomListContents(ListNum)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>ListNum<\/strong>: An integer parameter that specifies the index number of the custom list. In Excel, custom lists are indexed starting from 1. For example, built-in lists like days of the week or months may have specific indices, but user-defined lists are assigned indices based on their creation order. To determine the index of a custom list, you can check Excel&#8217;s options under &#8220;Advanced&#8221; > &#8220;General&#8221; > &#8220;Edit Custom Lists,&#8221; where lists are displayed in order, or use VBA to loop through lists programmatically. The method returns a string containing the list items, separated by commas.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example:<\/strong><br>Suppose you have a custom list in Excel containing the sequence &#8220;North, South, East, West&#8221; for sorting regional data. You can retrieve this list using xlwings to use it in a Python script for data analysis. Here&#8217;s a code example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\n\n# Connect to the active Excel application\napp = xw.apps.active\n\n# Assume the custom list is the first user-defined list (index might be 5 or higher, depending on built-in lists)\n# In practice, you might need to determine the index dynamically\nlist_num = 5 # Example index; adjust based on your Excel setup\nlist_contents = app.api.GetCustomListContents(list_num)\n\n# Output the retrieved list\nprint(\"Custom list contents:\", list_contents)\n\n# Split the string into a list for further processing\nitems = list_contents.split(',')\nprint(\"List items:\", items)\n\n# Use the list for sorting a pandas DataFrame, for instance\nimport pandas as pd\ndata = {'Region': &#91;'South', 'East', 'North', 'West']}\ndf = pd.DataFrame(data)\n# Create a categorical type based on the custom list for sorting\ndf&#91;'Region'] = pd.Categorical(df&#91;'Region'], categories=items, ordered=True)\ndf_sorted = df.sort_values('Region')\nprint(\"Sorted DataFrame:\")\nprint(df_sorted)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `GetCustomListContents` member of the `Application` object in Excel is a method that retrieves t&#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-1990","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/1990","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=1990"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/1990\/revisions"}],"predecessor-version":[{"id":3031,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/1990\/revisions\/3031"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=1990"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=1990"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=1990"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}