Home > OS >  Issues with openpyxl not reading all rows in sheet
Issues with openpyxl not reading all rows in sheet

Time:06-05

I cannot get openpyxl to iterate through all the rows I have in an excel notebook.

I am trying to get the code to print all values in a specific column, however, it just iterates over the 500 items and prints the first row 500 times.

This is my code so far:

import pandas as pd
import openpyxl
import requests

wb = openpyxl.load_workbook('search.xlsx')
ws = wb['Results']
df = pd.read_excel (r'search.xls', sheet_name='Results')

for cell in ws:
  y = ws.cell(row=2, column=1).hyperlink.target
  print(y)

Edit: Thanks to @topsail, I got the solution:

for row in ws.iter_rows(min_row=2):  
       print(row[0].hyperlink.target) 

CodePudding user response:

import openpyxl

wb = openpyxl.load_workbook('Search.xlsx')
ws = wb['Result']

for row in ws.iter_rows(min_row=2):
    try:
        print(row[0].hyperlink.target)
    except:
        print("no hyperlink!")
  • Related