Replace text in a workbook using Regular Expression
Aspose.Cells for Python via .NET provides the feature to replace text in a workbook using a regular expression. For this, the API provides ReplaceOptions.regex_key property of the ReplaceOptions class. Setting the ReplaceOptions.regex_key to true indicates that the searched key will be a regular expression.
The following code snippet demonstrates the use of the ReplaceOptions.regex_key property by using the sample excel file. The output file generated by the following code snippet is attached for reference.
Sample Code
from aspose.cells import ReplaceOptions, Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# Source directory | |
sourceDir = RunExamples.Get_SourceDirectory() | |
# Output directory | |
outputDir = RunExamples.Get_OutputDirectory() | |
workbook = Workbook(sourceDir + "SampleRegexReplace.xlsx") | |
replace = ReplaceOptions() | |
replace.case_sensitive = False | |
replace.match_entire_cell_contents = False | |
# Set to true to indicate that the searched key is regex | |
replace.regex_key = True | |
workbook.replace("\\bKIM\\b", "^^^TIM^^^", replace) | |
workbook.save(outputDir + "RegexReplace_out.xlsx") |