{"id":2355,"date":"2026-10-08T07:27:46","date_gmt":"2026-10-07T23:27:46","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2355"},"modified":"2026-03-29T09:49:59","modified_gmt":"2026-03-29T09:49:59","slug":"how-to-use-worksheetusedrange-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetusedrange-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.UsedRange in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>UsedRange<\/strong> property of a <strong>Worksheet<\/strong> object in Excel is a powerful feature that returns a <strong>Range<\/strong> object representing the area of a worksheet that is currently in use. This includes all cells that have ever contained data, formatting, or formulas, even if some of those cells are now empty. It is particularly useful for dynamically determining the extent of your data without needing to know the exact row and column boundaries beforehand. In xlwings, accessing this property allows you to programmatically interact with the actual used portion of a sheet, making your code more robust and adaptable to changing data sizes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax in xlwings:<\/strong><br>The UsedRange property is accessed as an attribute of a Worksheet object. There are no parameters to specify.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>used_range = ws.used_range<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here, <code>ws<\/code> is an xlwings Worksheet object (e.g., obtained via <code>wb.sheets['Sheet1']<\/code>). The returned <code>used_range<\/code> is an xlwings Range object. This Range object has properties like <code>.address<\/code>, <code>.row<\/code>, <code>.column<\/code>, <code>.rows.count<\/code>, and <code>.columns.count<\/code> which you can use to get the location and dimensions of the used area.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Functionality and Parameters:<\/strong><br>Since UsedRange is a property, not a method, it does not take direct parameters. Its behavior is controlled by the state of the Excel worksheet. However, understanding what constitutes the &#8220;used range&#8221; is crucial:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Inclusion Criteria:<\/strong> Any cell that has ever been edited, formatted, or contained a formula is considered part of the used range. This means clearing cell contents might not remove it from the used range if formatting remains.<\/li>\n\n\n\n<li><strong>Dynamic Nature:<\/strong> The used range can expand when new data is added at its edges and can sometimes be reset by saving the workbook after deleting all content and formatting from unused cells.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><br>Here are practical examples of using <code>ws.used_range<\/code> in xlwings:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Getting the Address and Dimensions:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\napp = xw.App(visible=False)\nwb = xw.Book('data.xlsx')\nws = wb.sheets&#91;0]\n\nused_rng = ws.used_range\nprint(f\"Used Range Address: {used_rng.address}\")\nprint(f\"Number of used rows: {used_rng.rows.count}\")\nprint(f\"Number of used columns: {used_rng.columns.count}\")\nprint(f\"Top-left cell: ({used_rng.row}, {used_rng.column})\")\n\nwb.close()\napp.quit()<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Clearing All Content and Formatting in the Used Range:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('report.xlsx')\nws = wb.sheets&#91;'Summary']\nws.used_range.clear() # Clears both content and formatting\nwb.save()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Dynamically Reading All Data into a Python List:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('inventory.xlsx')\nws = wb.sheets&#91;'Stock']\n\n# Read all values from the used range into a 2D list\nall_data = ws.used_range.value\nfor row in all_data:\n    print(row)\n\n# If you need the data as a pandas DataFrame\nimport pandas as pd\ndf = pd.DataFrame(all_data)\nprint(df.head())<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Applying Formatting to the Entire Used Area:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nfrom xlwings.constants import LineStyle\nwb = xw.Book('financials.xlsx')\nws = wb.sheets&#91;'Q1']\n\nused_rng = ws.used_range\n# Add borders around all used cells\nused_rng.api.Borders.LineStyle = LineStyle.xlContinuous\nused_rng.api.Borders.Weight = 2\nwb.save()<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The **UsedRange** property of a **Worksheet** object in Excel is a powerful feature that returns a *&#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-2355","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2355","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=2355"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2355\/revisions"}],"predecessor-version":[{"id":3590,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2355\/revisions\/3590"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2355"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2355"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2355"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}