How to Automate an Excel Sheet in Python? Last Updated : 12 Sep, 2025 Comments Improve Suggest changes 9 Likes Like Report Python provides openpyxl library using which you can automate Excel tasks programmatically. It allows you to read, write, edit, and format spreadsheets, apply formulas, and even generate charts directly from Python scripts saving both time and effort.Installing Required LibraryInstall it using the following command:pip install openpyxlExampleWe have an Excel sheet with product data:Column 3 → Prices (but they’re incorrect).We need to 1. Decrease each price by 10% 2.Save corrected prices in Column 4 3. Add a bar chart of the corrected pricesinput.pngDoing this manually for thousands of rows would take days - let's see how we can automate this using Python's openpyxl library:1. Import Libraries Python import openpyxl as xl from openpyxl.chart import BarChart, Reference openpyxl: Work with Excel.Bar Chart, Reference: Used to create charts.2. Open Workbook & Sheet Python #loads the Excel file python-spreadsheet.xlsx and picks the worksheet "Sheet1" from it. wb = xl.load_workbook("python-spreadsheet.xlsx") sheet = wb["Sheet1"] 3. Loop Through Rows Python #Iterating the sheet using for loop for row in range(2, sheet.max_row + 1): cell = sheet.cell(row, 3) 4. Correct the Prices Python corrected_price = float(cell.value.replace('$', '')) * 0.9 sheet.cell(row, 4).value = corrected_price Removes the $ sign, converts value to number.multiplies by 0.9 (10% discount) writes corrected price in Column 4.5. Add a Chart Python 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") chartSelects corrected prices (Column 4).Creates a bar chart and places it at cell E2.6. Save the File Python #Saves everything into a new file so original data isn’t lost. wb.save("python-spreadsheet2.xlsx") 7. Making It ReusableIf you want to process many spreadsheets, put the logic into a function: Python def process_workbook(filename): wb = xl.load_workbook(filename) 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 sheet.cell(row, 4).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(filename) Now you can run: Python process_workbook("python-spreadsheet.xlsx") Output output.pngComplete Code Python import openpyxl as xl from openpyxl.chart import BarChart, Reference # Load workbook & select sheet wb = xl.load_workbook("python-spreadsheet.xlsx") sheet = wb["Sheet1"] # Loop through rows (starting from 2 to skip header) for row in range(2, sheet.max_row + 1): cell = sheet.cell(row, 3) # Price column corrected_price = float(cell.value.replace('$', '')) * 0.9 sheet.cell(row, 4).value = corrected_price # Write to new column # Select corrected prices (Column 4) for chart values = Reference(sheet, min_row=2, max_row=sheet.max_row, min_col=4, max_col=4) # Create bar chart chart = BarChart() chart.add_data(values) sheet.add_chart(chart, "E2") # Place chart at cell E2 # Save updated file wb.save("python-spreadsheet2.xlsx") Related Articles:How to Learn Python from ScratchPython Projects – Beginner to AdvancedPython Interview Questions and Answers Create Quiz Comment A anuupadhyay Follow 9 Improve A anuupadhyay Follow 9 Improve Article Tags : Python python Explore Python FundamentalsPython Introduction 2 min read Input and Output in Python 4 min read Python Variables 4 min read Python Operators 4 min read Python Keywords 2 min read Python Data Types 8 min read Conditional Statements in Python 3 min read Loops in Python - For, While and Nested Loops 5 min read Python Functions 5 min read Recursion in Python 4 min read Python Lambda Functions 5 min read Python Data StructuresPython String 5 min read Python Lists 4 min read Python Tuples 4 min read Python Dictionary 3 min read Python Sets 6 min read Python Arrays 7 min read List Comprehension in Python 4 min read Advanced PythonPython OOP Concepts 11 min read Python Exception Handling 5 min read File Handling in Python 4 min read Python Database Tutorial 4 min read Python MongoDB Tutorial 3 min read Python MySQL 9 min read Python Packages 10 min read Python Modules 3 min read Python DSA Libraries 15 min read List of Python GUI Library and Packages 3 min read Data Science with PythonNumPy Tutorial - Python Library 3 min read Pandas Tutorial 4 min read Matplotlib Tutorial 5 min read Python Seaborn Tutorial 3 min read StatsModel Library - Tutorial 3 min read Learning Model Building in Scikit-learn 6 min read TensorFlow Tutorial 2 min read PyTorch Tutorial 6 min read Web Development with PythonFlask Tutorial 8 min read Django Tutorial | Learn Django Framework 7 min read Django ORM - Inserting, Updating & Deleting Data 4 min read Templating With Jinja2 in Flask 6 min read Django Templates 5 min read Build a REST API using Flask - Python 3 min read Building a Simple API with Django REST Framework 3 min read Python PracticePython Quiz 1 min read Python Coding Practice 1 min read Python Interview Questions and Answers 15+ min read Like