Automate Excel with Python using Openpyxl
By: Ali Shah Sohail
Tags: Openpyxl, Excel, Python

Recently Microsoft is considering adding Python as one of the official Excel scripting languages, according to a topic on Excel’s feedback hub opened last month, So in this article i m gonna show you how to automate excel with python using openpyxl library

Introduction of Openpyxl

Python provides the Openpyxl module, which is used to deal with Excel files without involving third-party Microsoft application software. By using this module, we can have control over excel without open the application. It is used to perform excel tasks such as read data from excel file, or write data to the excel file, draw some charts, accessing excel sheet, renaming sheet, modification (adding and deleting) in excel sheet, formatting, styling in the sheet, and any other task. Openpyxl is very efficient to perform these tasks for you.

Nowadays Data scientists often use the Openpyxl to perform different operations such as data copying to data mining as well as data analysis.

Working Process

The Openpyxl library is used to write or read the data in the excel file and many other tasks. An excel file that we use for operation is called Workbook A Workbook that contains multiple sheets

Getting Started

To get started, first we need a recent installation of the Python interpreter, which can be downloaded here. We then need to create a new project in our IDE (I use PyCharm, but any decent IDE works) and install the requests library. The easiest way to do this is using pip, the Python package manager

pip install openpyxl

All we need to do to get started is to create a new Python file and import the openpyxl library using

import openpyxl

from openpyxl import load_workbook



#import load_work method from openpyxl

Now i m going to shows how to get row count and column count by using openpyxl

def getRowCount(file, sheetName):

workbook = openpyxl.load_workbook(file)

sheet = workbook.get_sheet_by_name(sheetName)

return (sheet.max_row) def getColumnCount(file, sheetName):

workbook = openpyxl.load_workbook(file)

sheet = workbook.get_sheet_by_name(sheetName)

return (sheet.max_column)

The function code function required file path of the excel which you have to work and required sheet name of your workbook file, and this function will return maximum row and column count

Read Data from Excel File

def readData(file, sheetName, rownum, columnno):

workbook = openpyxl.load_workbook(file)

sheet = workbook.get_sheet_by_name(sheetName)

return sheet.cell(row=rownum, column=columnno).value

The readData function takes 4 arguments file which is path of your excel, sheet name will be your workbook sheet name, rownum will be particular row that has the data which you want to extract from it and columno is a vertical division of a sheet.

Write Data into Excel File

def writeData(file, sheetName, rownum, columnno, data):

workbook = openpyxl.load_workbook(file)

sheet = workbook.get_sheet_by_name(sheetName)

sheet.cell(row=rownum, column=columnno).value = data

workbook.save(file)

The writeData function takes 5 arguments file which is path of your excel, sheet name will be your workbook sheet name, rownum will be particular row that has the data which you want to extract from it, columno is a vertical division of a sheet and data that is the input from the user.

Create a new sheet if it doesn’t exists

def loadWorkbook(file, sheetName):

workbook = load_workbook(file)

if sheetName not in workbook.sheetnames:

workbook.create_sheet(sheetName)



workbook.save(file)

The above function takes two arguments and will create workbook sheet if it doesn’t exists

So here we see how to play with excel using python.

import XLUtils



path = "C:\\Users\\Ali Shah\\sample.xlsx"

sheetName = "Sheet1"

rows = XLUtils.getRowCount(path, sheetName)

column = XLUtils.getColumnCount(path, sheetName)

for r in range(1, rows + 5):

for c in range(1, column+5):

XLUtils.writeData(path, sheetName, r, c, "Hello Python")

So in the above code we use XLUtils Class to import all functions which we discuss in this article and simply we write Hello Python on Excel

Similarly we can aslo read data through excel too