In this post we will be creating a Python script that will update data in a spreadsheet using the openpyxl library. Openpyxl is a library that allows us to interact with Excel files in Python. It provides a range of tools for reading, writing, manipulating, and formatting Excel data. This makes it a useful tool for data analysis and reporting tasks.
See the sample of Python code below where we use the openpyxl library to update data within an existing spreadsheet. We start by loading the existing Excel file called
example.xlsx and assign it to a variable named
workbook. We then retrieve the active worksheet from the workbook, this is the worksheet displayed when the Excel file is opened. Next we modify and set the value of cell B2 to ’32’. Finally the changes made to the Excel file are saved using the
import openpyxl workbook = openpyxl.load_workbook('example.xlsx') worksheet = workbook.active worksheet['B2'] = 32 workbook.save('example.xlsx')
Take a look at some of our other content around the Python programming language by clicking here.