2

I'd like to read only the visible rows form an excel worksheet in python.

the input (excel sheet):

enter image description here

so when I filter for example:

enter image description here

as an output in python , I will get just the visible data (1 row) in this case.

here my code:

from openpyxl import load_workbook

wb = load_workbook(r'C:\Bureau\test\Data.xlsx') 
ws = wb['workload']

# iterate over all the rows in the sheet
for row in ws: 
    if ws.row_dimensions[row[0].row].hidden == False:
        for cell in row:
            print(cell.value)

the code works but it gives results in this format:

enter image description here

but I want to have with a normal format like a table or dataframe.

Any suggestions?

Thank you for your help

3
  • What do you mean by visible rows? Commented Feb 11, 2021 at 14:39
  • @ Mayank Porwal I mean by visible rows , that if I do a filter in excel than I will read the data in python , I will have just the visible rows that are not hidden because of the filter. Commented Feb 11, 2021 at 14:52
  • @ Mayank Porwal I 've tried this one but it's not giving me the data! stackoverflow.com/questions/31257353/… Commented Feb 11, 2021 at 14:56

1 Answer 1

7

To create a dataframe from the visible rows you could try something like this.

from openpyxl import load_workbook
import pandas as pd

wb = load_workbook(r'Data.xlsx') 
ws = wb['workload']
data = []

for row in ws: 
    if ws.row_dimensions[row[0].row].hidden == False:
      row_values = [cell.value for cell in row]
      data.append(row_values)

df = pd.DataFrame(data[1:], columns=data[0])

print(df)
Sign up to request clarification or add additional context in comments.

3 Comments

yes it's working thank you so much , this is what I want to have as a results!
@norie Thanks! That was helpful. But could you please explain what is row_dimensions[row[0].row]. When I googled, I only got info about row_dimensions that it is used to set the height and width of a row. But I didn't understand row[0].row. Also when I tried in my code, irrespective of index in row i.e., row[0], it gave me same/correct output. Eg: Even when I tried with row[3].row. So, it would be little helpful if you can explain row[0].row
@norie Could you please help me the same issue but when in readonly mode? When I used the solution, it gave me error saying AttributeError: 'ReadOnlyWorksheet' object has no attribute 'row_dimensions'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.