replace method
replace(input_file_name, output_file_name, pattern, replacement)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file.
def replace(self, input_file_name: str, output_file_name: str, pattern: str, replacement: str):
...
| Parameter | Type | Description |
|---|---|---|
| input_file_name | str | The input file name. |
| output_file_name | str | The output file name. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
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 in the input file, with the specified save format and additional options.
def replace(self, input_file_name: str, output_file_name: str, save_format: aspose.words.SaveFormat, pattern: str, replacement: str):
...
| Parameter | Type | Description |
|---|---|---|
| input_file_name | str | The input file name. |
| output_file_name | str | The output file name. |
| save_format | SaveFormat | The save format. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
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 in the input file, with the specified save format and additional options.
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):
...
| Parameter | Type | Description |
|---|---|---|
| input_file_name | str | The input file name. |
| output_file_name | str | The output file name. |
| save_format | SaveFormat | The save format. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
| options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
Returns
The number of replacements made.
replace(input_file_name, output_file_name, save_options, pattern, replacement)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file, with the specified save format and additional options.
def replace(self, input_file_name: str, output_file_name: str, save_options: aspose.words.saving.SaveOptions, pattern: str, replacement: str):
...
| Parameter | Type | Description |
|---|---|---|
| input_file_name | str | The input file name. |
| output_file_name | str | The output file name. |
| save_options | SaveOptions | The save options. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
Returns
The number of replacements made.
replace(input_file_name, output_file_name, save_options, pattern, replacement, options)
Replaces all occurrences of a specified character string pattern with a replacement string in the input file, with the specified save format and additional options.
def replace(self, input_file_name: str, output_file_name: str, save_options: aspose.words.saving.SaveOptions, pattern: str, replacement: str, options: aspose.words.replacing.FindReplaceOptions):
...
| Parameter | Type | Description |
|---|---|---|
| input_file_name | str | The input file name. |
| output_file_name | str | The output file name. |
| save_options | SaveOptions | The save options. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
| options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), each page of the output will be saved as a separate file. The specified output file name will be used to generate file names for each part following the rule: outputFile_partIndex.extension.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF file.
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 in the input stream, with the specified save format and additional options.
def replace(self, input_stream: io.BytesIO, output_stream: io.BytesIO, save_format: aspose.words.SaveFormat, pattern: str, replacement: str):
...
| Parameter | Type | Description |
|---|---|---|
| input_stream | io.BytesIO | The input stream. |
| output_stream | io.BytesIO | The output stream. |
| save_format | SaveFormat | The save format. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), only the first page of the output will be saved to the specified stream.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF to the specified stream.
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 in the input stream, with the specified save format and additional options.
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):
...
| Parameter | Type | Description |
|---|---|---|
| input_stream | io.BytesIO | The input stream. |
| output_stream | io.BytesIO | The output stream. |
| save_format | SaveFormat | The save format. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
| options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), only the first page of the output will be saved to the specified stream.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF to the specified stream.
Returns
The number of replacements made.
replace(input_stream, output_stream, save_options, pattern, replacement)
Replaces all occurrences of a specified character string pattern with a replacement string in the input stream, with the specified save format and additional options.
def replace(self, input_stream: io.BytesIO, output_stream: io.BytesIO, save_options: aspose.words.saving.SaveOptions, pattern: str, replacement: str):
...
| Parameter | Type | Description |
|---|---|---|
| input_stream | io.BytesIO | The input stream. |
| output_stream | io.BytesIO | The output stream. |
| save_options | SaveOptions | The save options. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), only the first page of the output will be saved to the specified stream.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF to the specified stream.
Returns
The number of replacements made.
replace(input_stream, output_stream, save_options, pattern, replacement, options)
Replaces all occurrences of a specified character string pattern with a replacement string in the input stream, with the specified save format and additional options.
def replace(self, input_stream: io.BytesIO, output_stream: io.BytesIO, save_options: aspose.words.saving.SaveOptions, pattern: str, replacement: str, options: aspose.words.replacing.FindReplaceOptions):
...
| Parameter | Type | Description |
|---|---|---|
| input_stream | io.BytesIO | The input stream. |
| output_stream | io.BytesIO | The output stream. |
| save_options | SaveOptions | The save options. |
| pattern | str | A string to be replaced. |
| replacement | str | A string to replace all occurrences of pattern. |
| options | FindReplaceOptions | FindReplaceOptions object to specify additional options. |
Remarks
If the output format is an image (BMP, EMF, EPS, GIF, JPEG, PNG, or WebP), only the first page of the output will be saved to the specified stream.
If the output format is TIFF, the output will be saved as a single multi-frame TIFF to the specified stream.
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
- module aspose.words.lowcode
- class Replacer