replace method

replace(input_file_name, output_file_name, pattern, replacement)

Replaces all occurrences of a specified character string pattern with a replacement string.

def replace(self, input_file_name: str, output_file_name: str, pattern: str, replacement: str):
    ...
ParameterTypeDescription
input_file_namestrThe input file name.
output_file_namestrThe output file name.
patternstrA string to be replaced.
replacementstrA string to replace all occurrences of pattern.

Returns

The number of replacements made.

replace(input_file_name, output_file_name, save_format, pattern, replacement)

Replaces all occurrences of a specified character string pattern with a replacement string.

def replace(self, input_file_name: str, output_file_name: str, save_format: aspose.words.SaveFormat, pattern: str, replacement: str):
    ...
ParameterTypeDescription
input_file_namestrThe input file name.
output_file_namestrThe output file name.
save_formatSaveFormatThe save format.
patternstrA string to be replaced.
replacementstrA string to replace all occurrences of pattern.

Returns

The number of replacements made.

replace(input_file_name, output_file_name, save_format, pattern, replacement, options)

Replaces all occurrences of a specified character string pattern with a replacement string.

def replace(self, input_file_name: str, output_file_name: str, save_format: aspose.words.SaveFormat, pattern: str, replacement: str, options: aspose.words.replacing.FindReplaceOptions):
    ...
ParameterTypeDescription
input_file_namestrThe input file name.
output_file_namestrThe output file name.
save_formatSaveFormatThe save format.
patternstrA string to be replaced.
replacementstrA string to replace all occurrences of pattern.
optionsFindReplaceOptionsFindReplaceOptions object to specify additional options.

Returns

The number of replacements made.

replace(input_stream, output_stream, save_format, pattern, replacement)

Replaces all occurrences of a specified character string pattern with a replacement string.

def replace(self, input_stream: io.BytesIO, output_stream: io.BytesIO, save_format: aspose.words.SaveFormat, pattern: str, replacement: str):
    ...
ParameterTypeDescription
input_streamio.BytesIOThe input stream.
output_streamio.BytesIOThe output stream.
save_formatSaveFormatThe save format.
patternstrA string to be replaced.
replacementstrA string to replace all occurrences of pattern.

Returns

The number of replacements made.

replace(input_stream, output_stream, save_format, pattern, replacement, options)

Replaces all occurrences of a specified character string pattern with a replacement string.

def replace(self, input_stream: io.BytesIO, output_stream: io.BytesIO, save_format: aspose.words.SaveFormat, pattern: str, replacement: str, options: aspose.words.replacing.FindReplaceOptions):
    ...
ParameterTypeDescription
input_streamio.BytesIOThe input stream.
output_streamio.BytesIOThe output stream.
save_formatSaveFormatThe save format.
patternstrA string to be replaced.
replacementstrA string to replace all occurrences of pattern.
optionsFindReplaceOptionsFindReplaceOptions object to specify additional options.

Returns

The number of replacements made.

Examples

Shows how to replace string in the document.

# There is a several ways to replace string in the document:
doc = MY_DIR + 'Footer.docx'
pattern = '(C)2006 Aspose Pty Ltd.'
replacement = 'Copyright (C) 2024 by Aspose Pty Ltd.'
options = aw.replacing.FindReplaceOptions()
options.find_whole_words_only = False
aw.lowcode.Replacer.replace(input_file_name=doc, output_file_name=ARTIFACTS_DIR + 'LowCode.Replace.1.docx', pattern=pattern, replacement=replacement)
aw.lowcode.Replacer.replace(input_file_name=doc, output_file_name=ARTIFACTS_DIR + 'LowCode.Replace.2.docx', save_format=aw.SaveFormat.DOCX, pattern=pattern, replacement=replacement)
aw.lowcode.Replacer.replace(input_file_name=doc, output_file_name=ARTIFACTS_DIR + 'LowCode.Replace.3.docx', save_format=aw.SaveFormat.DOCX, pattern=pattern, replacement=replacement, options=options)

Shows how to replace string in the document using documents from the stream.

# There is a several ways to replace string in the document using documents from the stream:
pattern = '(C)2006 Aspose Pty Ltd.'
replacement = 'Copyright (C) 2024 by Aspose Pty Ltd.'
with system_helper.io.FileStream(MY_DIR + 'Footer.docx', system_helper.io.FileMode.OPEN, system_helper.io.FileAccess.READ) as stream_in:
    with system_helper.io.FileStream(ARTIFACTS_DIR + 'LowCode.ReplaceStream.1.docx', system_helper.io.FileMode.CREATE, system_helper.io.FileAccess.READ_WRITE) as stream_out:
        aw.lowcode.Replacer.replace(input_stream=stream_in, output_stream=stream_out, save_format=aw.SaveFormat.DOCX, pattern=pattern, replacement=replacement)
    with system_helper.io.FileStream(ARTIFACTS_DIR + 'LowCode.ReplaceStream.2.docx', system_helper.io.FileMode.CREATE, system_helper.io.FileAccess.READ_WRITE) as stream_out:
        options = aw.replacing.FindReplaceOptions()
        options.find_whole_words_only = False
        aw.lowcode.Replacer.replace(input_stream=stream_in, output_stream=stream_out, save_format=aw.SaveFormat.DOCX, pattern=pattern, replacement=replacement, options=options)

See Also