Automation in Python

What is automation?

There are several tasks available that can be done easily with the help of feature automation in python. It refers to the process which takes place with the help of python codes or designing a program in python that does the work automatically. This saves a lot of time for the user and is more efficient and helps to overcome a lot of work in a less span of time.

Let us see an example to get the things cleared,

Here we will update an excel file with a new price with the help of python.

First, we need to design an excel sheet in which we will be working. Here we are taking an excel sheet with members as products and price. We will design a code in python that will return the updated price and a graph.

ZCq7y NmkZFjfLO3jkHhlTGwqnqC1f6osk8IJX2eJnfyL3HhiI iAbGikpG7Otstl2BYnMSt2HwAph2DHKQZ0KgAilpXe5xjwFTZqMqaX0pVmO2LlMNLePV5 GB LT2H0OhZ0w

Now let’s design a code which will return the update price and a graph.

Explanation:

  • We will install the openpyxl package. It can be done by typing it in the terminal of pycharm as: pip install openpyxl.

Where it has already been installed on my pc.

  • For operating xl sheets in python we need to import this package or else the program will not run. The package is imported as: import openpyxl as xl , where xl will act as the object of the package.
  • For the operation in graph we also need to import the graph package as: from openpyxl.chart import Barchart, Reference.
  • Then we will design a separate function for the operation where we will receive the .xlsx file.
  • We will load the file and the sheet in separate variables for working in the operation.
  • Then we will start a loop from 2 to (max row+1). We are taking 2 as the values are stored in the file from the 2 rows and will go on to the (max row+1) i.e., till the last row getting incremented by 1 in every operation of the loop.
  • Inside the loop we will provide the cell location with the value of the loop as row location and the default column location of this file as 3rd, where the value in the cell will get updated by multiplying with 5 and get stored in the 4th column.
  • Then after the termination of the loop we will provide the values of the graph in the reference function with the values of the sheet the desired minimum row, maximum row and the desired minimum column add maximum column.
  • The Barchart function is called, the values are added and the chart is then initialized to the sheet as the desired location.
  • We save the workbook with a different file name as we don’t want to rewrite it in the same file.
  • In the end the function is called with the file to run the operation.

Note: the excel sheet should be present inside the current running project in pycharm. We are using pycharm to run the python program.

Example Of Python Automation:

import openpyxl as xl
from openpyxl.chart import BarChart, Reference

def auto_sheet(spread):
    wb=xl.load_workbook(spread)
    sheet=wb['Sheet1']

    for row in range(2, sheet.max_row+1):
        cell=sheet.cell(row,3)
        updatevalue=cell.value*5
        updatedvalue_cell=sheet.cell(row,4)
        updatedvalue_cell.value=updatevalue

    values=Reference(sheet, min_row=2, max_row=sheet.max_row, min_col=4, max_col=4)
    chart= BarChart()
    chart.add_data(values)
    sheet.add_chart(chart, 'E2')


    wb.save('transaction_updated.xlsx')


auto_sheet('transaction.xlsx')

Output:

The output of the program:

JZfBDtoRf4l36HbMTQNdbkcF VbUupIas1UNqzFl162ZwsN3nOJWu5Pm MaWbvjiutCJ33VqGrCo6lrQDwoBLEShLiSF6QXK8RLhgXNYg 0gtXd6ZkjptEKe8YrBE3LoiuQqg

The output of the updated excel sheet:

wWrXAOU OYe4Ns1OYJB4 sS xvN 7AF9BEhIIewoHeWDJDvLQ7lVcIYqYf1wc2VILZYTgeeXHsbiqVwLpdNHSh5u9imBjsjBuQA2LQcQDeJIzUwC vH gt5iwuAG0EgdIYBSKA

This is an example to demonstrate the process of automation in python. There are several other examples such as sending email, replying to email etc.

Here we come to the end of the topic. Hope we were able to explain the topic properly.

Please comment and share this post if you find any helpful and wants to improve WhatsApp us.

Share Your Love
Default image
Soham Malakar
Hello my name is Soham Malakar am currently pursuing BCA Coding is one of my favorite hobbies. I love to code and an enthusiast to learn different types of programming languages. I love to get intoxicated in the world of technology and video games. A quick learner possessing better results.
Articles: 36

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply