{"id":13,"date":"2026-01-24T11:17:24","date_gmt":"2026-01-24T03:17:24","guid":{"rendered":"https:\/\/openpyxl.com\/blog\/?p=13"},"modified":"2026-01-16T06:00:49","modified_gmt":"2026-01-16T06:00:49","slug":"how-to-traverse-rowscolumns-using-openpyxl","status":"publish","type":"post","link":"https:\/\/openpyxl.com\/blog\/how-to-traverse-rowscolumns-using-openpyxl.html","title":{"rendered":"How To Traverse Rows\/Columns Using OpenPyXL?"},"content":{"rendered":"<h2>Method<\/h2>\n<p style=\"margin-top: 4pt; margin-bottom: 4pt; font-family: \u5fae\u8f6f\u96c5\u9ed1; font-size: 14.0pt;\">Use the `rows` property to iterate over all rows with data, and `columns` to iterate over all columns with data. The `values` property provides access to the data in the sheet\u2019s cell range.<\/p>\n<h2>Sample Code<\/h2>\n\n\n<pre class=\"wp-block-code\"><code>#Iterating through Rows and Columns\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 each cell in the first row\nfor cell in ws&#91;'1']:\n    print(cell.value)    #Output the value of each cell\n\n#Iterate through each cell in the first column\nfor cell in ws&#91;'A']:\n    print(cell.value)    #Output the value of each cell\n\n#Iterate through the first to third rows\nfor row in ws&#91;'1:3']:\n    for cell in row:      #Iterate through each row's cells\n        print(cell.value)    #Output the value of each cell\n\n#Iterate through the first to third columns\nfor column in ws&#91;'A:C']:\n    for cell in column:   #Iterate through each column's cells\n        print(cell.value)    #Output the value of each cell\n\n#Iterate through all rows using the rows attribute of the worksheet\nfor row in ws.rows:\n    line=&#91;cell.value for cell in row]\n    print (line)\n\n#Iterate through all columns using the columns attribute of the worksheet\nfor column in ws.columns:\n    line = &#91;cell.value for cell in column]\n    print (line)\n\n#Use the values attribute to return data from each row, returned as a list\nfor row in ws.values:\n    print(row)\n\n#Output as a list\nfor row in ws.values:\n    print(list(row))\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-13","post","type-post","status-publish","format-standard","hentry","category-openpyxl-worksheet"],"_links":{"self":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/13","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=13"}],"version-history":[{"count":2,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions"}],"predecessor-version":[{"id":218,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/posts\/13\/revisions\/218"}],"wp:attachment":[{"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/media?parent=13"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/categories?post=13"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openpyxl.com\/blog\/wp-json\/wp\/v2\/tags?post=13"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}