Search code examples
pandasfor-loopopenpyxlordereddictionarypandas.excelwriter

Openpyxl - How to read the same cell in all of the sheets within a file?


I'd like to loop through all the sheets in my Excel file, read the content of cell B2 from each sheet, and print it out. Later, I plan to set conditions based on the string value of that cell.

  • The cell of interest is B2: I would like to print the three different student names, which are Roberto, Miquel Angelo and Leonardo.
  • My current piece of code is only printing 'Leonardo', (Sheet_3).
# Importing modules
import openpyxl as op
import pandas as pd
import numpy as np
import xlsxwriter
import openpyxl
from openpyxl import Workbook, load_workbook

# Defining the file path
file_path = r'C:/Users/machukovich/Desktop/stackoverflow.xlsx'

# Load workbook as openpyxl
reference_workbook = openpyxl.load_workbook(file_path)

# We will mantain the workbook open
wb = reference_workbook.active

# Getting the sheetnames as a list using the sheetnames attribute

sheet_names = reference_workbook.sheetnames
print(sheet_names)

# Loading the file into a dictionary of Dataframes
dict_of_df = pd.read_excel(file_path, sheet_name=None, skiprows=2)

# Reading up the B2 cell for later use:
student_name = wb['B2'].value
for sheet_names in dict_of_df:
    print(student_name)
  • Below, you can find snapshots of the Excel file to further clarification.

enter image description here enter image description here enter image description here


Solution

  • Just doing the following should be all you need.
    Included list to add names to as well.

    from openpyxl import load_workbook
    
    file_path = r'C:/Users/machukovich/Desktop/stackoverflow.xlsx'
    reference_workbook = load_workbook(file_path)
    
    names_list = []
    for sheet in reference_workbook.worksheets:
        student_name = sheet['B2'].value
        print(f"Student Name: {student_name}")
        names_list.append(student_name)
    
    print(f"List of Names: {names_list}")
    

    As noted by @Max888
    If you are importing load_workbook from openpyxl, you don't need to specify that module in the command, i.e.;
    openpyxl.load_workbook(file_path) should just be load_workbook(file_path)