{"id":2344,"date":"2026-10-02T15:29:27","date_gmt":"2026-10-02T07:29:27","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2344"},"modified":"2026-03-29T09:38:11","modified_gmt":"2026-03-29T09:38:11","slug":"how-to-use-worksheetrange-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetrange-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Range in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Range<\/code> member of the <code>Worksheet<\/code> object in the xlwings API is a fundamental and versatile tool for interacting with cells, ranges, and data within an Excel worksheet. It is the primary gateway for reading, writing, and manipulating cell content, formatting, and formulas. Functionally, it allows you to select a specific cell, a rectangular block of cells, or even non-contiguous cells, enabling operations like data input, extraction, and formatting adjustments.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax and Parameters:<\/strong><br>The basic syntax to access a range is through a <code>Worksheet<\/code> object. The most common call is:<br><code>ws.range(cell1, cell2=None)<\/code><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ws<\/code>: The xlwings <code>Worksheet<\/code> object.<\/li>\n\n\n\n<li><code>cell1<\/code>: A required argument that defines the start of the range. It can be a string address (e.g., <code>\"A1\"<\/code>), a tuple of integers for the row and column (e.g., <code>(1, 1)<\/code> for A1), or an xlwings <code>Range<\/code> object.<\/li>\n\n\n\n<li><code>cell2<\/code>: An optional argument that defines the end of the range for a multi-cell selection. It accepts the same formats as <code>cell1<\/code>. If omitted, the range is a single cell defined by <code>cell1<\/code>.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">You can also use the shorthand indexing syntax: <code>ws[\"A1\"]<\/code> or <code>ws[\"A1:C5\"]<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Properties and Methods of the Range Object:<\/strong><br>Once you have a <code>Range<\/code> object, you can use its properties and methods. Key members include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>.value<\/code>: Gets or sets the values in the range. For a single cell, it returns a scalar (e.g., number, string). For multiple cells, it returns a nested list (list of lists).<\/li>\n\n\n\n<li><code>.formula<\/code>: Gets or sets the Excel formula string for the range.<\/li>\n\n\n\n<li><code>.address<\/code>: Returns the absolute address of the range as a string.<\/li>\n\n\n\n<li><code>.row<\/code> \/ <code>.column<\/code>: Return the first row or column number of the range (1-indexed).<\/li>\n\n\n\n<li><code>.rows<\/code> \/ <code>.columns<\/code>: Return a <code>Range<\/code> object representing the rows or columns within the range.<\/li>\n\n\n\n<li><code>.autofit()<\/code>: Automatically adjusts the width of columns or height of rows to fit their contents.<\/li>\n\n\n\n<li><code>.clear()<\/code>: Clears the content and formatting of the range.<\/li>\n\n\n\n<li><code>.copy(destination)<\/code>: Copies the range to a specified destination range.<\/li>\n\n\n\n<li><code>.paste()<\/code>: Pastes the contents of the Clipboard into the range.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Code Examples:<\/strong><\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Reading and Writing Values:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book(\"data.xlsx\")\nws = wb.sheets&#91;\"Sheet1\"]\n\n# Write a single value\nws.range(\"B2\").value = \"Total Revenue\"\n\n# Write a list of lists (2D array)\ndata = &#91;&#91;1, 2, 3], &#91;4, 5, 6]]\nws.range(\"A4\").value = data # Expands from A4 to C5\n\n# Read a single cell\nrevenue = ws.range(\"D10\").value\nprint(revenue)\n\n# Read a contiguous range into a 2D list\ntable_data = ws.range(\"A1:C10\").value\nfor row in table_data:\n    print(row)<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Using Formulas and Formatting:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Insert a SUM formula\nws.range(\"C15\").formula = \"=SUM(C5:C14)\"\n\n# Autofit columns for a specific range\nws.range(\"A1:F20\").autofit()\n\n# Clear a range\nws.range(\"OldData!A1:Z100\").clear()<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Working with Rows and Columns:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Get the address of a dynamically defined range\nstart_cell = (5, 2) # Row 5, Column 2 (B5)\nend_cell = (15, 5) # Row 15, Column 5 (E15)\nmy_range = ws.range(start_cell, end_cell)\nprint(f\"Range address: {my_range.address}\")\n\n# Access entire rows\/columns\nws.range(\"5:5\").value = &#91;\"Q1\", \"Q2\", \"Q3\", \"Q4\"] # Entire row 5\nws.range(\"C:C\").column_width = 15 # Set width of entire column C<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Range` member of the `Worksheet` object in the xlwings API is a fundamental and versatile tool &#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-2344","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2344","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=2344"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2344\/revisions"}],"predecessor-version":[{"id":3573,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2344\/revisions\/3573"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}