Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Issues with openpyxl not reading all rows in sheet

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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) 

>Solution :

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!")
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading