Reading an excel file using Python openpyxl module

08 Jun, 2021
Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The openpyxl module allows Python program to read and modify Excel files.For example, users might have to go through thousands of rows and pick out a few handful of information to make small changes based on some criteria. Using Openpyxl module, these tasks can be done very efficiently and easily.Use this command to install openpyxl module :  
sudo pip3 install openpyxl 
 Input file :  
Code #1 : Program to print the particular cell value  
Python3
# Python program to read an excel file # import openpyxl moduleimport openpyxl # Give the location of the filepath = "C:\\Users\\Admin\\Desktop\\demo.xlsx" # To open the workbook# workbook object is createdwb_obj = openpyxl.load_workbook(path) # Get workbook active sheet object# from the active attributesheet_obj = wb_obj.active # Cell objects also have a row, column,# and coordinate attributes that provide# location information for the cell. # Note: The first row or# column integer is 1, not 0. # Cell object is created by using# sheet object's cell() method.cell_obj = sheet_obj.cell(row = 1, column = 1) # Print value of cell object# using the value attributeprint(cell_obj.value)
Output :  
STUDENT 'S NAME
  Code #2 : Determine total number of rows  
Python3
# import openpyxl moduleimport openpyxl # Give the location of the filepath = "C:\\Users\\Admin\\Desktop\\demo.xlsx" # to open the workbook# workbook object is createdwb_obj = openpyxl.load_workbook(path)sheet_obj = wb_obj.active # print the total number of rowsprint(sheet_obj.max_row)
Output :  
6
  Code #3 : Determine total number of columns  
Python3
# importing openpyxl moduleimport openpyxl # Give the location of the filepath = "C:\\Users\\Admin\\Desktop\\demo.xlsx" # workbook object is createdwb_obj = openpyxl.load_workbook(path) sheet_obj = wb_obj.active # print total number of columnprint(sheet_obj.max_column)
Output :  
4
  Code #4 : Print all columns name  
Python3
# importing openpyxl moduleimport openpyxl # Give the location of the filepath = "C:\\Users\\Admin\\Desktop\\demo.xlsx" # workbook object is createdwb_obj = openpyxl.load_workbook(path) sheet_obj = wb_obj.activemax_col = sheet_obj.max_column # Loop will print all columns namefor 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  
Python3
# importing openpyxl moduleimport openpyxl # Give the location of the filepath = "C:\\Users\\Admin\\Desktop\\demo.xlsx" # workbook object is createdwb_obj = openpyxl.load_workbook(path) sheet_obj = wb_obj.activem_row = sheet_obj.max_row # Loop will print all values# of first columnfor 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 : Print a particular row value  
Python3
# importing openpyxl moduleimport openpyxl # Give the location of the filepath = "C:\\Users\\Admin\\Desktop\\demo.xlsx" # workbook object is createdwb_obj = openpyxl.load_workbook(path) sheet_obj = wb_obj.active max_col = sheet_obj.max_column # Will print a particular row valuefor 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
 
surinderdawra388
python-modules
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Different ways to create Pandas Dataframe
Enumerate() in Python
Python String | replace()
How to Install PIP on Windows ?
*args and **kwargs in Python
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Convert string dictionary to dictionary