{"id":2305,"date":"2026-09-13T07:00:49","date_gmt":"2026-09-12T23:00:49","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2305"},"modified":"2026-03-28T12:46:18","modified_gmt":"2026-03-28T12:46:18","slug":"how-to-use-worksheetcells-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetcells-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Cells in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Cells<\/code> property of a <code>Worksheet<\/code> object in the Excel object model is a fundamental member for accessing and manipulating individual cells or ranges of cells within a worksheet. In xlwings, this is accessed through the <code>api<\/code> property, which provides direct access to the underlying Excel object model, allowing for precise control similar to VBA. The <code>Cells<\/code> property is versatile, enabling both reading and writing of cell values, as well as formatting and other cell-specific operations.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality:<\/strong><br>The primary function of the <code>Cells<\/code> property is to return a <code>Range<\/code> object that represents a single cell or a collection of cells. It is commonly used to refer to cells by their row and column numbers, which is particularly useful in loops or when programmatically determining cell positions. This property is essential for tasks that require iterating over cells, dynamically referencing ranges, or accessing cells based on calculated indices.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax:<\/strong><br>In xlwings, the syntax to access the <code>Cells<\/code> property is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>worksheet.api.Cells(row_index, column_index)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>row_index<\/code>: Required. An integer that specifies the row number of the cell (1-indexed).<\/li>\n\n\n\n<li><code>column_index<\/code>: Required. An integer that specifies the column number of the cell (1-indexed). Alternatively, a string representing the column letter (e.g., &#8220;A&#8221;) can be used, but when using the <code>Cells<\/code> property directly via <code>api<\/code>, it typically expects numeric indices for consistency with the Excel object model.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>Cells<\/code> property can also be called with a single argument to return a range encompassing all cells in the worksheet, though this is less common. For example, <code>worksheet.api.Cells<\/code> without arguments refers to all cells, but in practice, <code>worksheet.used_range<\/code> or similar methods are often preferred for performance.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Examples:<\/strong><br>Here are several xlwings API code examples demonstrating the use of the <code>Cells<\/code> property:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Accessing a Single Cell Value:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>import xlwings as xw\nwb = xw.Book('example.xlsx')\nws = wb.sheets&#91;'Sheet1']\n# Get value from cell at row 5, column 3 (C5)\ncell_value = ws.api.Cells(5, 3).Value\nprint(cell_value)\n# Set value in cell at row 2, column 1 (A2)\nws.api.Cells(2, 1).Value = \"Hello, World!\"<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Iterating Over a Range of Cells:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Write values to the first 5 rows in column A\nfor i in range(1, 6):\n    ws.api.Cells(i, 1).Value = f\"Data {i}\"\n# Read values from the first 3 rows in column B\nfor i in range(1, 4):\n    print(ws.api.Cells(i, 2).Value)<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Formatting Cells:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Change the font color of cell D10 to red\nws.api.Cells(10, 4).Font.Color = 0xFF0000 # RGB color for red\n# Set the interior color of cell E5 to yellow\nws.api.Cells(5, 5).Interior.Color = 0xFFFF00<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Using Cells with Variables for Dynamic References:<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>row_num = 7\ncol_num = 4\n# Dynamically access cell at row 7, column 4 (D7)\ndynamic_cell = ws.api.Cells(row_num, col_num)\ndynamic_cell.Value = \"Dynamic Entry\"<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>Accessing All Cells (Entire Worksheet):<\/strong><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code># Refer to all cells in the worksheet (use with caution for large sheets)\nall_cells = ws.api.Cells\nprint(f\"Total rows: {all_cells.Rows.Count}, Total columns: {all_cells.Columns.Count}\")<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Cells` property of a `Worksheet` object in the Excel object model is a fundamental member for 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-2305","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2305","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=2305"}],"version-history":[{"count":1,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2305\/revisions"}],"predecessor-version":[{"id":3514,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2305\/revisions\/3514"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2305"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2305"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2305"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}