{"id":14,"date":"2026-01-24T17:21:18","date_gmt":"2026-01-24T09:21:18","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=14"},"modified":"2026-01-16T06:03:00","modified_gmt":"2026-01-16T06:03:00","slug":"how-to-traverse-regions-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-traverse-regions-using-openpyxl.html","title":{"rendered":"How To Traverse Regions Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p>Use a nested `for` loop to iterate over the range A1:C3 and output the data in each cell.<\/p>\n<p>&gt;&gt;&gt; for row in ws[&#8220;A1:C3&#8221;]:\u00a0\u00a0 # Iterate through the rows in the range<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for cell in row:\u00a0\u00a0\u00a0 # Iterate through each cell in the rows<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 print(cell.value)<\/p>\n<p>The following code saves the data from the specified range into the list `data` and outputs the data.<\/p>\n<p>&gt;&gt;&gt; data = []<\/p>\n<p>&gt;&gt;&gt; for row in ws[&#8220;A1:C3&#8221;]:<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 rv = []<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 for cell in row:<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 rv.append(cell.value)<\/p>\n<p>\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 data.append(rv)<\/p>\n<p>&gt;&gt;&gt; print(data)<\/p>\n<p>The following properties provide the bounds of the data area in the worksheet:<\/p>\n<ul>\n<li>min_row\uff1athe minimum row number of the data area<\/li>\n<li>min_column\uff1athe minimum column number of the data area<\/li>\n<li>max_row\uff1athe maximum row number of the data area<\/li>\n<li>max_column\uff1athe maximum column number of the data area<\/li>\n<li>The `iter_rows` and `iter_cols` methods allow for iterating through specific rows and columns within a given range.<\/li>\n<\/ul>\n<h2>Sample\u00a0 Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Iterating Through Cell Ranges\n\n#Import load_workbook function\nfrom openpyxl import Workbook\n\n#Create a new workbook\nwb=Workbook()\n#Get the active worksheet\nws=wb.active\n\n#Iterate through the rows within a cell range\nfor row in ws&#91;'A1:C3']:\n    #Iterate through the cells of each row within a cell range\n    for cell in row:\n        print(cell.value)    #Output the value of each cell\n\n#Save the data from a specified range into a list called data\ndata = &#91;]\nfor row in ws&#91;\"A1:C3\"]:\n    rv = &#91;]\n    for cell in row:\n        rv.append(cell.value)\n        data.append(rv)\n#Output the list\nprint(data)\n\n#Get and output data using the range property\n#provided by the worksheet object\nwb=load_workbook('test.xlsx')\nws=wb.active\nprint(&#91;ws.min_row,ws.max_row,ws.min_column,ws.max_column])\n\nfor row in ws.iter_rows(min_row=3, max_col=4, max_row=5):\n    line = &#91;cell.value for cell in row]\n    print (line)\n\nfor col in ws.iter_cols(min_row=3, max_col=4, max_row=5):\n    line = &#91;cell.value for cell in col]\n    print (line)\n\nwb.save('test.xlsx')\nwb.close()<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>OpenPyXL<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-14","post","type-post","status-publish","format-standard","hentry","category-openpyxl-worksheet"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/14","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/comments?post=14"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions"}],"predecessor-version":[{"id":220,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/14\/revisions\/220"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=14"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=14"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=14"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}