Update cell value using openpyxl in Python

Hello friends, you know how to read cell value in openpyxl in Python. In this tutorial, I will tell you how you can update cell values using openpyxl in Python.

Update cell value using openpyxl in Python

I have created a sample Excel file 'Cust_Details'. It contains two columns 'CustID' and 'Name'.

Sample file before updation :

Update cell value using openpyxl in Python

Now I want to change the name of the customer present in cell 'B6'. At first, I have imported the necessary modules to my code. You can check our tutorial on how to install openpyxl in Python to install the module on your local system. Now using the load_workbook() function I have provided the name of the Excel file I want to update and stored it in a temporary variable, wb. After loading the workbook it’s necessary to activate it to enable usage. You can do this using the active attribute. As I want to update the cell 'B6' I have indexed my sheet to the specified cell and assigned it an appropriate value. After the updation, I saved the file under the same name by using the save() function.

from openpyxl import load_workbook

wb = load_workbook('Cust_Details.xlsx')

sheet = wb.active

sheet['B6'] = "Heena Potnuru"

wb.save('Cust_Details.xlsx')

Sample file after updation :

Update cell value using openpyxl in Python

Now you can successfully, update a cell’s value of an Excel sheet using openpyxl in Python.

Leave a Reply

Your email address will not be published. Required fields are marked *