Skip to main content

Arcadis is Hiring!!!

 ARCADIS IS HIRING!!! Company : Arcadia Position : Software Engineer Experience : Bs/Ms Degree : BS in CS Apply Link: Apply Here

Automate an excel using Python

We all know that Python is ruling all over the world, and we also know that Python is beginner’s friendly and it’s easy to learn in comparison to other languages. One of the best things you can do with Python is Automation.



Why we require to automate python with excel?

Consider a scenario that you’re asked to create an account on a website for 30,000 employees. How would you feel? Surely you will be frustrated doing this task manually and repeatedly.

Now just imagine the life of employees who are into the data entry jobs. Their job is to take the data from tables such as Excel or Google Sheet and insert it somewhere else. They browse different websites and magazine, they collect the data from there, and then they insert it into the database. They also need to do the calculations for the entries.

Generally, the wages is based on the performance in this work. More entries, higher the wages.

In this blog, we will create a small project to learn automation in Python. "If you’re a beginner then you may prefer to watch some videos to learn the automation in Python and reading this blog might be a boring task for you but here we will go through step by step to explain everything in detail and to make things easier for you".


Introduction of Task:-


In the above excel sheet, we have the recorded some data for all kinds of transactions, but let’s say due to an error, the price for the product listed in the 3rd column is wrong. We need to decrease the price by 10%. You can do this task manually by using a mathematical formula in the 4th column but it will take huge time if there are thousands of data.


Now Lets start the Main Coding part:-

there are so many libraries available for python to work on in different number of task, but we will be using the library for automation of excel via python is openpyxl.

For installing the library we are using pip:-  pip install openpyxl

Now lets have a look what our code actually look like:-

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

wb = xl.load_workbook('python-spreadsheet.xlsx')
sheet = wb['Sheet1']

for row in range(2, sheet.max_row + 1):
cell = sheet.cell(row, 3)
corrected_price = float(cell.value.replace('$','')) * 0.9
corrected_price_cell = sheet.cell(row, 4)
corrected_price_cell.value = corrected_price

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('python-spreadsheet2.xlsx')

Lets understand this:-

Step 1:- We have install  or import a package named openpyxl and also we need to import BarChart,                  References. This BarChart library is used for adding the chart.

Step 2:- Now we need to load the excel sheet for that we are creating a variable wb  and now store that                   sheet with the filename in function braces.

Step 3:-  To use the column 2 to 4 we have to create a loop in it. We are saving our entries in variable                    named cell.

Step 4:-  Now we need to calculate the corrected prices. So we are multiplying the values saved in the                   cell variable with 0.9. Once the calculation is done we need to add all the corrected prices in                   a new column. To add a new column we will get a reference to the cell in the given                                  row but in the fourth column. Once the cell is created, we need to set the corrected price                       values in this cell .
Step 5:-  Now we need to save all updated entries and the chart we have created in the above code. We                will save this in a new file python-spreadsheet2.xlsx because we don’t’ want to accidentally                   overwrite the original file in case our program has a bug.

               Run your program. A newly updated file python-spreadhsheet2.xlsx  will be                                             created for you with updated prices and charts.

Step 6:-  This program is only relying on a specific file that is python-spreadsheet.xlsx.

             To make it work for several spreadsheets we will reorganize this code, and we will move the                 code inside a function. This function will take the name of the file as an input and it will                         execute the process. Below is the updated code for the same.

Comments

Popular posts from this blog

Let's Start with the Internet Marketing | Affiliate Marketing

Digital Marketing is  that the act of promoting and mercantilism merchandise and services by leverage online selling ways like social media selling, search selling, and email marketing. At a high level, digital marketing refers to advertising delivered through digital channels like search engines, websites, social media, email, and mobile apps. Using these online media channels, digital marketing is that the method by which companies endorse goods, services, and brands. Consumers heavily depend upon digital means to research products. as an example, Think with Google marketing insights found that 48% of consumers start their inquiries on search engines, while 33% look to brand websites and 26% search within mobile applications. While modern-day digital marketing is an unlimited system of channels to which marketers simply must onboard their brands, advertising online is way more complex than the channels alone....

Hewlett Packard Enterprise

CMS INTERN JOB   At Hewlett Packard Enterprise, we bring together the brightest minds to create breakthrough technology solutions that advance the way people live and work. What sets us apart? Our people and our relentless dedication to helping our customers make their mark on the world. We are a team of doers, dreamers and visionaries; inspired by our purpose and driven by our strategy. We live by our three values: partner, innovate and act. Our legacy inspires us as we forge ahead, always pushing to discover what’s next. Every day is a new opportunity to advance and grow ourselves, our company and the industry. Some people call it an obsession, we call it a way of life. SOME INFORMATIONS AND BASIC REQUIREMENTS REGARDING THE JOB: Job ID: 1104914 Date Posted: 10/26/2021 Primary Location: Bangalore, Karnataka Other Locations: Bangalore, Karnataka, India,Chennai,TAMILNADU, India Job Category: Graduate Program and Internships Schedule: Full time Shift: No shift premium (India) Respons...