Python | Reading Excel file using openpyxl module



For example, a user might have to go through thousands of lines and select some useful information to make small changes based on some criteria. Using the Openpyxl module, these tasks can be done very efficiently and easily.

Use this command to install the openpyxl module:

 sudo pip3 install openpyxl  

Input file:

Code # 1: Program for printing a specific cell value

# Python Excel file reader

  
# module import openpyxl

import openpyxl

 
# Specify file location

path = "C: Users Admin Desktop demo.xlsx"

 
# To open the workbook
# the workbook object has been created

wb_obj = openpyxl.load_workbook (path)

 
# Get the book of the active sheet of the object
# from active attribute

sheet_obj = wb_obj.active

 
# Cell objects also have row, column,
# and coordinates of the attributes that provide
# location information for the cell.

  
# Note: first row or
Column integer is 1 , not 0.

 
# The cell object is created using
# cell () method of the sheet object.

cell_obj = sheet_obj.cell (row = 1 , column = 1 )

 
# Output the value of the cell object
# using the value attribute

print (cell_obj.value)

Output:

 
 STUDENT `S NAME 

Code # 2: Determine the total number of lines

# module import openpyxl

import openpyxl

  
# Specify file location

path = "C: Users Admin Desktop demo.xlsx"

 
# open book
# workbook object created

wb_obj = openpyxl.load_workbook (path)

sheet_obj = wb_obj.active

  
# print the total number of lines

print (sheet_obj.max_row)

Output:

 6 

Code # 3: Determine the total number of columns

# import of the openpyxl module

import openpyxl

 
# Specify file location

path = "C: Users Admin Desktop demo.xlsx"

 
# workbook object created

wb_obj = openpyxl.load_workbook (path)

 

sheet_obj = wb_obj.active

 
# ptint total number of columns

print (sheet_obj.max_column)

Output:

 4 

Code # 4: Print all column names

# importing the openpyxl module

import openpyxl

 
# Specify file location

path = "C: Users Admin Desktop demo.xlsx"

 
# workbook object created

wb_obj = openpyxl.load_workbook (path)

 

sheet_obj = wb_obj.active

max_col = sheet_obj.max_column

 
# Loop will print all column names

for i in range ( 1 , max_col + 1 ):

cell_obj = sheet_obj.cell (row = 1 , column = i )

print (cell_obj.value )

Output:

 STUDENT `S NAME COURSE BRANCH SEMESTER 

Code # 5: Print first column value

# import openpyxl module

import openpyxl

 
 # Specify file location

path = " C: Users Admin Desktop demo.xlsx "

  
# workbook object created

wb_obj = openpyxl .load_workbook (path)

 

sheet_obj = wb_obj.active

m_row = sheet_obj.max_row

 
# Loop will print all values ​​
# of the first column

for i in range ( 1 , m_row + 1 ):

cell_obj = sheet_obj.cell (row = i, column = 1 )

  print ( cell_obj.value)

Output:

 STUDENT `S NAME ANKIT RAI RAHUL RAI PRIYA RAI AISHWARYA HARSHITA JAISWAL 

Code # 6: printing a specific string value

# import module openpyxl

import openpyxl

  
# Specify file location

path = " C: Users Admin Desktop demo.xlsx "

  
# workbook object created

wb_obj = openpyxl.load_workbook (path)

  

sheet_obj = wb_obj.active

 

max_col = sheet_obj.max_column

 
# Prints a definition this string value

for i in range ( 1 , max_col + 1 ):

cell_obj = sheet_obj.cell (row = 2 , column = i)

print (cell_obj.value, end = "" )

Output:

 ANKIT RAI B.TECH CSE 4