{"id":2345,"date":"2026-10-03T07:20:20","date_gmt":"2026-10-02T23:20:20","guid":{"rendered":"https:\/\/xlwings.net\/blog\/?p=2345"},"modified":"2026-03-29T09:41:41","modified_gmt":"2026-03-29T09:41:41","slug":"how-to-use-worksheetrows-in-the-xlwings-api-way","status":"publish","type":"post","link":"https:\/\/xlwings.net\/blog\/how-to-use-worksheetrows-in-the-xlwings-api-way\/","title":{"rendered":"How to use Worksheet.Rows in the xlwings API way"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <code>Rows<\/code> member of the <code>Worksheet<\/code> object in xlwings provides access to a collection representing all the rows within a specific worksheet. This is a powerful feature for performing operations on entire rows, such as selecting, formatting, hiding, or deleting them. It is analogous to the <code>Rows<\/code> property in the Excel VBA object model and allows for efficient, row-level manipulations without needing to iterate through individual cells manually.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Functionality<\/strong><br>The primary function of the <code>Rows<\/code> property is to return a <code>Range<\/code> object that represents one or more rows. You can use it to apply formatting (like font color or row height), adjust visibility, clear contents, or delete rows entirely. It is particularly useful for applying uniform changes across a row or for dynamically handling rows based on conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Syntax<\/strong><br>The basic syntax to access rows in xlwings is:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.rows<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This returns a <code>Range<\/code> object for all rows in the worksheet <code>ws<\/code>. You can also specify a particular row or a range of rows using indexing or slicing:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>ws.rows[0]<\/code> or <code>ws.rows[1]<\/code>: Accesses the first row. Note: xlwings uses 1-based indexing by default for rows and columns when using the <code>rows<\/code> property in this context, aligning with Excel&#8217;s row numbering (row 1, row 2, etc.).<\/li>\n\n\n\n<li><code>ws.rows[1:5]<\/code>: Accesses rows 1 through 4 (the slice is 1-based and end-exclusive, similar to Python slicing, but here it refers to Excel rows 1 to 4).<\/li>\n\n\n\n<li><code>ws.rows['1:5']<\/code>: Alternative string notation to access rows 1 to 5 (inclusive).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">Once you have the <code>Range<\/code> object, you can call various methods and properties. Common parameters include:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>For formatting: Use properties like <code>row_height<\/code>, <code>font<\/code>, <code>color<\/code>, or methods like <code>autofit()<\/code>.<\/li>\n\n\n\n<li>For operations: Use methods like <code>delete()<\/code>, <code>clear()<\/code>, <code>hide<\/code>, or <code>unhide<\/code>.<\/li>\n\n\n\n<li>To get values: Use the <code>value<\/code> property to retrieve data as a list of lists.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Examples<\/strong><br>Here are some practical xlwings API code examples using the <code>Rows<\/code> member:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Selecting and Formatting All Rows<\/strong>: Set a uniform row height for the entire worksheet.<\/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']\nws.rows.row_height = 20 # Sets height of all rows to 20 points<\/code><\/pre>\n\n\n\n<ol start=\"2\" class=\"wp-block-list\">\n<li><strong>Hiding Specific Rows<\/strong>: Hide rows 5 to 10 based on a condition.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.rows&#91;'5:10'].hidden = True # Hides rows 5 through 10<\/code><\/pre>\n\n\n\n<ol start=\"3\" class=\"wp-block-list\">\n<li><strong>Deleting Rows<\/strong>: Remove rows 3 to 7 from the worksheet.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.rows&#91;'3:7'].delete() # Deletes rows 3 to 7, shifting rows up<\/code><\/pre>\n\n\n\n<ol start=\"4\" class=\"wp-block-list\">\n<li><strong>Clearing Contents<\/strong>: Clear data from the first 5 rows without deleting the rows.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.rows&#91;:5].clear_contents() # Clears values but keeps formatting (using 0-based slicing for the first 5 rows)\n# Alternatively, with 1-based: ws.rows&#91;'1:5'].clear_contents()<\/code><\/pre>\n\n\n\n<ol start=\"5\" class=\"wp-block-list\">\n<li><strong>Autofitting Rows<\/strong>: Adjust row heights to fit the content automatically.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>ws.rows.autofit() # Autofits all rows based on cell content<\/code><\/pre>\n\n\n\n<ol start=\"6\" class=\"wp-block-list\">\n<li><strong>Iterating Over Rows<\/strong>: Loop through each row to perform custom operations, such as checking values.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>for row in ws.rows:\n    if row.value and row.value&#91;0] == 'Target': # Check first cell in each row\nrow.color = (255, 200, 200) # Highlight row with a light red color<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The `Rows` member of the `Worksheet` object in xlwings provides access to a collection representing &#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-2345","post","type-post","status-publish","format-standard","hentry","category-xlwings-api-reference"],"_links":{"self":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2345","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=2345"}],"version-history":[{"count":2,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2345\/revisions"}],"predecessor-version":[{"id":3575,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/posts\/2345\/revisions\/3575"}],"wp:attachment":[{"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/media?parent=2345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/categories?post=2345"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlwings.net\/blog\/wp-json\/wp\/v2\/tags?post=2345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}