Python | Reading an excel file using openpyxl module

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, user might have to go through thousands of rows and pick out few handful 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to read an excel file
  
# import openpyxl module
import openpyxl
  
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
  
# To open the workbook 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
  
# Get workbook active sheet object
# from the active attribute
sheet_obj = wb_obj.active
  
# Cell objects also have 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 attribute
print(cell_obj.value)

chevron_right


Output :

STUDENT 'S NAME

 
Code #2 : Determine total number of rows

filter_none

edit
close

play_arrow

link
brightness_4
code

# import openpyxl module
import openpyxl
  
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
  
# to open the workbook 
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active
  
# print the total number of rows
print(sheet_obj.max_row)

chevron_right


Output :

6

 
Code #3 : Determine total number of columns

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing openpyxl module
import openpyxl
  
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
  
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
  
sheet_obj = wb_obj.active
  
# ptint total number of column 
print(sheet_obj.max_column)

chevron_right


Output :

4

 
Code #4 : Print all columns name

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing openpyxl module
import openpyxl
  
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
  
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
  
sheet_obj = wb_obj.active
max_col = sheet_obj.max_column
  
# Loop will print all columns name
for i in range(1, max_col + 1):
    cell_obj = sheet_obj.cell(row = 1, column = i)
    print(cell_obj.value)

chevron_right


Output :

STUDENT 'S NAME
COURSE
BRANCH
SEMESTER

 
Code #5 : Print first column value

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing openpyxl module
import openpyxl
  
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
  
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
  
sheet_obj = wb_obj.active
m_row = sheet_obj.max_row
  
# Loop will print all values
# of first column 
for i in range(1, m_row + 1):
    cell_obj = sheet_obj.cell(row = i, column = 1)
    print(cell_obj.value)

chevron_right


Output :

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

 
Code #6 : Print a particular row value

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing openpyxl module
import openpyxl
  
# Give the location of the file
path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
  
# workbook object is created
wb_obj = openpyxl.load_workbook(path)
  
sheet_obj = wb_obj.active
  
max_col = sheet_obj.max_column
  
# Will print a particular row value
for i in range(1, max_col + 1):
    cell_obj = sheet_obj.cell(row = 2, column = i)
    print(cell_obj.value, end = " ")

chevron_right


Output :

ANKIT RAI B.TECH CSE 4


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.