検索と置換
キーボードとマウスを使用して文書内を簡単に移動できますが、スクロールするページが多い場合、長い文書内で特定のテキストを見つけるのにかなりの時間がかかります。文書内で使用した特定の文字や単語を置き換える場合は、さらに時間がかかります。 「検索と置換」機能を使用すると、文書内で一連の文字を検索し、別の一連の文字に置き換えることができます。
Aspose.Words を使用すると、Microsoft Word などの追加のアプリケーションをインストールして使用しなくても、ドキュメント内で特定の文字列または正規表現パターンを検索し、代替文字列に置き換えることができます。これにより、多くの入力や書式設定のタスクが高速化され、作業時間を節約できる可能性があります。
この記事では、メタキャラクターのサポートを使用して文字列置換と正規表現を適用する方法について説明します。
{#ways-to-find-and-replace} を検索して置換する方法
Aspose.Words では、次の 2 つの方法で検索と置換操作を適用できます。
- 単純な文字列置換 – 特定の文字列を検索して別の文字列に置換するには、すべての出現に従って別の指定された置換文字列に置換される検索文字列 (英数字) を指定する必要があります。どちらの文字列にも記号を含めることはできません。文字列の比較では大文字と小文字が区別される場合があること、またはスペルがわからない場合や、類似したスペルが複数ある場合があることを考慮してください。
- 正規表現 – 正規表現を指定して、完全に一致する文字列を検索し、正規表現に従って置換します。単語は英数字のみで構成されるものとして定義されることに注意してください。単語全体のみが一致する置換が実行され、入力文字列にたまたま記号が含まれている場合、フレーズは検出されません。
さらに、単純な文字列置換および正規表現とともに特別なメタキャラクタを使用して、検索および置換操作内でブレークを指定できます。
Aspose.Words は、aspose.words.replacing モジュールを使用して検索および置換機能を提供します。 FindReplaceOptions クラスを使用して、検索と置換のプロセス中に多くのオプションを使用できます。
単純な文字列置換 {#find-and-replace-text-using-simple-string-replacement} を使用したテキストの検索と置換
replace メソッドと replace_regex メソッドのいずれかを使用して、特定の文字列を検索または置換し、行われた置換の数を返すことができます。この場合、置換する文字列、その出現箇所すべてを置換する文字列、置換で大文字と小文字を区別するかどうか、および単独の単語のみが影響を受けるかどうかを指定できます。
次のコード例は、文字列「CustomerName」を検索し、文字列 「James Bond」 に置き換える方法を示しています。
doc = aw.Document()
builder = aw.DocumentBuilder(doc)
builder.writeln("Hello _CustomerName_,")
print("Original document text: " + doc.range.text)
doc.range.replace("_CustomerName_", "James Bond", aw.replacing.FindReplaceOptions(aw.replacing.FindReplaceDirection.FORWARD))
print("Document text after replace: " + doc.range.text)
# Save the modified document
doc.save(docs_base.artifacts_dir + "FindAndReplace.simple_find_replace.docx")
単純な文字列置換を適用する前に、ドキュメントの違いに気づくことができます。

単純な文字列置換を適用した後:

正規表現を使用したテキストの検索と置換
正規表現は、特定のテキストのシーケンスを記述するパターンです。単語が 2 回出現するすべての単語を 1 つの単語に置き換えるとします。次に、正規表現 ([a-zA-Z]+) \1
を適用してダブルワード パターンを指定できます。
replace_regex メソッドを使用して、一致を見つけるパターンとして正規表現パラメータを設定することで、特定の文字の組み合わせを検索および置換します。
次のコード例は、正規表現パターンに一致する文字列を指定された置換文字列で置換する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.writeln("sad mad bad") | |
options = aw.replacing.FindReplaceOptions() | |
doc.range.replace_regex("[s|m]ad", "bad", options) | |
doc.save(docs_base.artifacts_dir + "FindAndReplace.replace_with_regex.docx") |
正規表現による文字列置換を適用する前に、ドキュメントの違いに気づくことができます。

そして、正規表現による文字列置換を適用した後、次のようになります。

メタキャラクターを使用した文字列の検索と置換
特定のテキストまたはフレーズが複数の段落、セクション、またはページで構成されている場合は、検索文字列または置換文字列でメタキャラクターを使用できます。メタキャラクタの中には、段落区切りの &p、セクション区切りの &b、ページ区切りの &m、改行の &l などがあります。
次のコード例は、テキストを段落と改ページに置き換える方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
builder.font.name = "Arial" | |
builder.writeln("First section") | |
builder.writeln(" 1st paragraph") | |
builder.writeln(" 2nd paragraph") | |
builder.writeln("insert-section") | |
builder.writeln("Second section") | |
builder.writeln(" 1st paragraph") | |
findReplaceOptions = aw.replacing.FindReplaceOptions() | |
findReplaceOptions.apply_paragraph_format.alignment = aw.ParagraphAlignment.CENTER | |
# Double each paragraph break after word "section", add kind of underline and make it centered. | |
count = doc.range.replace("section&p", "section&p----------------------&p", findReplaceOptions) | |
# Insert section break instead of custom text tag. | |
count = doc.range.replace("insert-section", "&b", findReplaceOptions) | |
doc.save(docs_base.artifacts_dir + "FindAndReplace.replace_text_containing_meta_characters.docx") |
ドキュメントのヘッダー/フッターの文字列を検索して置換する
HeaderFooter クラスを使用すると、Word 文書のヘッダー/フッター セクション内のテキストを検索して置換できます。
次のコード例は、ドキュメント内のヘッダー セクションのテキストを置換する方法を示しています。
ヘッダー文字列の置換を適用する前に、ドキュメントの違いに気づくことができます。

ヘッダー文字列の置換を適用した後:

ドキュメント内のフッター セクションのテキストを置換するコード例は、前のヘッダー コード例とよく似ています。次の 2 行を置き換えるだけです。
header = headersFooters.get_by_header_footer_type(aw.HeaderFooterType.HEADER_PRIMARY)
header.range.replace("Aspose.Words", "Remove", options)
以下のとおりです。
header = headersFooters.get_by_header_footer_type(aw.HeaderFooterType.FOOTER_PRIMARY)
header.range.replace("Aspose.Words", "Remove", options)
フッター文字列の置換を適用する前に、ドキュメントの違いに気づくことができます。

フッター文字列置換を適用した後:

{#ignore-text-during-find-and-replace} の検索と置換中にテキストを無視する
検索と置換操作を適用する際、テキストの特定のセグメントを無視できます。したがって、テキストの特定の部分を検索から除外し、残りの部分にのみ検索と置換を適用することができます。
Aspose.Words には、ignore_deleted、ignore_fields、ignore_inserted などのテキストを無視するための検索および置換プロパティが多数用意されています。
次のコード例は、削除リビジョン内のテキストを無視する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
builder = aw.DocumentBuilder(doc) | |
# Insert non-revised text. | |
builder.writeln("Deleted") | |
builder.write("Text") | |
# Remove first paragraph with tracking revisions. | |
doc.start_track_revisions("author", DateTime.now) | |
doc.first_section.body.first_paragraph.remove() | |
doc.stop_track_revisions() | |
options = aw.replacing.FindReplaceOptions() | |
options.ignore_deleted = True | |
doc.range.replace_regex("e", "*", options) | |
print(doc.get_text()) | |
options.ignore_deleted = False | |
doc.range.replace(regex, "*", options) | |
print(doc.get_text()) |
検索と置換操作 {#customize-find-and-replace-operation} をカスタマイズする
Aspose.Words は、apply_font および apply_paragraph_formats プロパティによる特定の形式の適用、use_substitutions プロパティによる置換パターンでの置換の使用など、テキストを検索および置換するためのさまざまなプロパティを提供します。
次のコード例は、文書内の特定の単語を強調表示する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "Find and highlight.docx") | |
options = aw.replacing.FindReplaceOptions() | |
options.direction = aw.replacing.FindReplaceDirection.BACKWARD | |
options.apply_font.highlight_color = drawing.Color.yellow | |
text = "your document" | |
doc.range.replace(text, text, options) | |
doc.save(docs_base.artifacts_dir + "FindAndReplace.find_and_highlight.docx") |