Get values of all rows in a particular column in openpyxl in Python
In this openpyxl tutorial, we will learn how we can get values of all rows in a particular column using openpyxl library in Python.
Program to get values of all rows in a particular column using openpyxl
Here, in this program, we will use the openpyxl library to do the operation which contains various modules for creating, modifying, and reading Excel files.
Let’s take a sample Excel workbook with some value in it for the program.
Our aim is to display the values of all the rows of a particular column of the active sheet.
Step1: Firstly, let’s import openpyxl library to our program.
Step2: Load the Excel workbook to the program by specifying the file’s path. Here, we will use the load_workbook() method of the openpyxl library for this operation.
wb = openpyxl.load_workbook("//home//sanamsahoo0876//Documents//Book1.xlsx")
Step3: Create a variable and assign it with the active sheet’s reference by giving the sheet’s name.
sh = wb["Sheet1"]
Step4: Iterate through cells of a column which values are to be printed on the console. Here we have taken the first column ‘A‘ to get all the values of the rows of this column.
for col in sh['A']: print(col.value)
Here is the complete Python Program:
import openpyxl wb = openpyxl.load_workbook("//home//sanamsahoo0876//Documents//Book1.xlsx") sh = wb["Sheet1"] for col in sh['A']: print(col.value)
Student_ID 1901568086 1901568087 1901568088 1901568089 1901568090 1901568091
We have successfully got all the values of the row of the particular column.
I hope you have enjoyed reading this article and learned how we can get the values of all rows of a column using openpyxl in Python.
You can also read, How to read cell value in openpyxl in Python
Leave a Reply