How to get values of all rows in a particular column in openpyxl in Python?


In this article, we will show you how to get all the row values of a particular column in an excel file using the python openpyxl library.

Assume we have taken an excel file with the name sampleTutorialsPoint.xlsx containing some random data. We will return all the row values of a given particular column in an excel file.

sampleTutorialsPoint.xlsx

Player Name Age Type Country Team Runs Wickets
Virat Kohli 33 Batsman India Royal Challengers Bangalore 6300 20
Bhuvaneshwar Kumar 34 Batsman India Sun Risers Hyderabad 333 140
Mahendra Singh Dhoni 39 Batsman India Chennai Super Kings 4500 0
Rashid Khan 28 Bowler Afghanistan Gujarat Titans 500 130
Hardik Pandya 29 All rounder India Gujarat Titans 2400 85
David Warner 34 Batsman Australia Delhi Capitals 5500 12
Kieron Pollard 35 All rounder West Indies Mumbai Indians 3000 67
Rohit Sharma 33 Batsman India Mumbai Indians 5456 20
Kane Williamson 33 Batsman New Zealand Sun Risers Hyderabad 3222 5
Kagiso Rabada 29 Bowler South Africa Lucknow Capitals 335 111

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Use the import keyword, to import the openpyxl module (Openpyxl is a Python package for interacting with and managing Excel files. Excel 2010 and later files with the xlsx/xlsm/xltx/xltm extensions are supported. Data scientists use Openpyxl for data analysis, data copying, data mining, drawing charts, styling sheets, formula addition, and other operations)

pip install openpyxl
  • Create a variable to store the path of the input excel file.

  • To create/load a workbook, pass the input excel file as an argument to the openpyxl module's load_workbook() function (loads a workbook).

  • Access the specific sheet of the workbook by giving the sheet name as the index to the workbook object.

  • Pass the column index to the worksheet and traverse through all the rows of the column.

  • Print the values of each row in that column

Example

The following program prints all the row values of a particular column (here A)in an excel file −

# importing openpyxl module import openpyxl # input excel file path inputExcelFile ="sampleTutorialsPoint.xlsx" # creating or loading an excel workbook newWorkbook = openpyxl.load_workbook(inputExcelFile) # Accessing specific sheet of the workbook. firstWorksheet = newWorkbook["Sheet1"] # Passing the column index to the worksheet and traversing through the each row of the column for column_data in firstWorksheet['E']: # Printing the column values of every row print(column_data.value)

Output

On executing, the above program will generate the following output

Team
Royal Challengers Bangalore
Sun Risers Hyderabad
Chennai Super Kings
Gujarat Titans
Gujarat Titans
Delhi Capitals
Mumbai Indians
Mumbai Indians
Sun Risers Hyderabad
Lucknow Capitals

We utilized a sample excel file with dummy data in our software. Using the openpyxl module, we loaded the excel sheet as a workbook and selected the first sheet by passing the sheet name as the index to the workbook Object. The column index was then passed as an index to the worksheet, which iterated over all of the column values and printed them.

Conclusion

We learned how to use the openpyxl module to fetch the excel file as a workbook and get the specified sheet as a worksheet by index. We learned how to retrieve all the values of the rows of a specific column, which is useful in many applications such as retrieving college names, student names, and so on in excel files.

Updated on: 18-Aug-2022

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements