This article guides on how to find and replace text in Excel using Python. It has all the details to set the IDE, a list of instructions, and a sample code to find and replace multiple words in Excel using Python. You will learn to customize the search operation using various parameters exposed by the API.
Steps to Replace a Word in Excel using Python
- Set the IDE to use Aspose.Cells for Python via Java to find and replace text
- Start JVM using the jpype library
- Import the classes License and Workbook
- Load the license to avoid watermarks
- Load the input workbook
- Create the FindOptions object and set the lookin type and look at type
- Parse through all the worksheets and replace text in each found cell
- Save the output Excel file after updating the text
These steps summarize the process for text find and change in Excel using Python. Start the JVM, import necessary classes, load a license and source Workbook, and create a FindOptions object to customize the searching. Iterate through all the sheets in the loaded Workbook, find the occurrence of cells having the specified text, and replace them with new text before saving the output Excel file.
Code to Replace Data in Excel using Python
This code snippet demonstrates how to find and replace multiple values in Excel using Python. The default settings in FindOptions are case insensitive, whole sheet, values only, and forward direction. However, it supports limiting to formulas rather than values, doing partial match instead of full-match, searching backwards, and many more.
This article has taught us the process to find text values, formula, comments, etc. in an Excel file and replace the content with a new value. To read an Excel file, refer to the article Read Excel file in Python.