Using Excel Spreadsheets using Python

Openpyxl is a Python library that provides various methods to interact with Excel Files using Python.

It allows operations like reading, writing, arithmetic operations, plotting graphs, etc.

Firstly, We need to install the library using below command.

pip install openpyxl

  • To read an Excel file you have to open the spreadsheet using the load_workbook() method.
  • After that, you can use the active to select the first sheet available and the cell attribute to select the cell by passing the row and column parameter.

CODE:

import openpyxl

path = "gfg.xlsx"

# To open the workbook

# workbook object is created

wb_obj = openpyxl.load_workbook(path)

sheet obj = wb_obj.active

row = sheet_obj.max_row

column = sheet_obj.max_column

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

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

print(cell_obj.value)

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

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

print(cell_obj.value, end = " ")

  • We can get the count of the total rows and columns using the max_row and max_column respectively.


Sign In or Register to comment.