Change language

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 

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically