カーソルによるナビゲーション
ドキュメントを操作するときは、ドキュメントが短くても長くても、ドキュメント内を移動する必要があります。仮想カーソルを使用したナビゲーションは、ドキュメント内の異なるノード間を移動する機能を表します。
短いドキュメント内では、キーボードの矢印キーを使用するか、マウスをクリックして挿入ポイントを任意の場所に配置することによっても挿入ポイントを移動できるため、ドキュメント内での移動は簡単です。しかし、多くのページがある大きな文書を作成した場合、これらの基本的なテクニックでは不十分になります。
この記事では、文書内を移動し、仮想カーソルを使用して文書の別の部分に移動する方法について説明します。
現在のカーソル位置の検出
ドキュメント内を移動するプロセスを開始する前に、現在選択されているノードを取得する必要があります。 current_node プロパティを使用すると、選択したノードのカーソルの正確な位置を取得できます。さらに、現在のノードを取得する代わりに、current_paragraph プロパティと current_section プロパティを使用して、現在選択されている段落または現在選択されているセクションを取得できます。
DocumentBuilder を使用して実行する挿入操作はすべて、current_node の前に挿入されます。現在の段落が空であるか、カーソルが段落の終わりの直前に位置している場合、current_node は None を返します。
ドキュメント内のメソッドの移動
テキストを編集するときは、ドキュメント内を移動する方法とドキュメント内のどこに正確に移動するかを知っておくことが重要です。 Aspose.Words を使用すると、文書内を移動して、そのさまざまなセクションや部分に移動できます。これは、スクロールせずに Word 文書内のページまたは見出しに移動する Microsoft Word のナビゲーション ウィンドウの機能に似ています。
主な方法は、カーソル位置をドキュメント内の特定のノードに移動できるようにすることです。これは、move_to メソッドを使用して実現できます。
次のコード例は、DocumentBuilder をドキュメント内の別のノードに移動する方法を示しています。
# 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) | |
# Start a bookmark and add content to it using a DocumentBuilder. | |
builder.start_bookmark("MyBookmark") | |
builder.writeln("Bookmark contents.") | |
builder.end_bookmark("MyBookmark") | |
# The node that the DocumentBuilder is currently at is past the boundaries of the bookmark. | |
self.assertEqual(doc.range.bookmarks[0].bookmark_end, builder.current_paragraph.first_child) | |
# If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this. | |
builder.move_to_bookmark("MyBookmark") | |
# Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it. | |
self.assertEqual(doc.range.bookmarks[0].bookmark_start, builder.current_paragraph.first_child) | |
# We can move the builder to an individual node, | |
# which in this case will be the first node of the first paragraph, like this. | |
builder.move_to(doc.first_section.body.first_paragraph.get_child_nodes(aw.NodeType.ANY, False)[0]) | |
self.assertEqual(aw.NodeType.BOOKMARK_START, builder.current_node.node_type) | |
self.assertTrue(builder.is_at_start_of_paragraph) | |
# A shorter way of moving the very start/end of a document is with these methods. | |
builder.move_to_document_end() | |
self.assertTrue(builder.is_at_end_of_paragraph) | |
builder.move_to_document_start() | |
self.assertTrue(builder.is_at_start_of_paragraph) |
ただし、基本的な move_to メソッド以外にも、より具体的なメソッドがあります。
ドキュメントの先頭または末尾に移動する
move_to_document_start および move_to_document_end メソッドを使用して、ドキュメントの先頭または末尾に移動できます。
次のコード例は、カーソル位置をドキュメントの先頭または末尾に移動する方法を示しています。
# 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) | |
# Move the cursor position to the beginning of your document. | |
builder.move_to_document_start() | |
print("\nThis is the beginning of the document.") | |
# Move the cursor position to the end of your document. | |
builder.move_to_document_end() | |
print("\nThis is the end of the document.") |
ブックマークを使用してナビゲートする
探したい場所にマークを付けて、再度簡単に移動できます。ドキュメントにブックマークを必要な数だけ挿入し、一意の名前でブックマークを識別してブックマーク内を移動できます。 move_to_bookmark メソッドを使用してブックマークに移動できます。
次のコード例は、カーソル位置をブックマークに移動する方法を示しています。
# 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) | |
# Start a bookmark and add content to it using a DocumentBuilder. | |
builder.start_bookmark("MyBookmark") | |
builder.writeln("Bookmark contents.") | |
builder.end_bookmark("MyBookmark") | |
# The node that the DocumentBuilder is currently at is past the boundaries of the bookmark. | |
self.assertEqual(doc.range.bookmarks[0].bookmark_end, builder.current_paragraph.first_child) | |
# If we wish to revise the content of our bookmark with the DocumentBuilder, we can move back to it like this. | |
builder.move_to_bookmark("MyBookmark") | |
# Now we're located between the bookmark's BookmarkStart and BookmarkEnd nodes, so any text the builder adds will be within it. | |
self.assertEqual(doc.range.bookmarks[0].bookmark_start, builder.current_paragraph.first_child) | |
# We can move the builder to an individual node, | |
# which in this case will be the first node of the first paragraph, like this. | |
builder.move_to(doc.first_section.body.first_paragraph.get_child_nodes(aw.NodeType.ANY, False)[0]) |
表のセルに移動します
move_to_cell メソッドを使用して表のセルに移動できます。このメソッドを使用すると、特定のテーブル内の任意のセルにカーソルを移動できるようになります。さらに、インデックスを指定して、move_to_cell メソッド内のセル内の任意の位置または指定した文字にカーソルを移動できます。
次のコード例は、カーソル位置を指定した表のセルに移動する方法を示しています。
# 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 + "Tables.docx") | |
builder = aw.DocumentBuilder(doc) | |
# Move the builder to row 3, cell 4 of the first table. | |
builder.move_to_cell(0, 2, 3, 0) | |
builder.write("\nCell contents added by DocumentBuilder") | |
table = doc.get_child(aw.NodeType.TABLE, 0, True).as_table() | |
self.assertEqual(table.rows[2].cells[3], builder.current_node.parent_node.parent_node) | |
self.assertEqual("Cell contents added by DocumentBuilderCell 3 contents\a", table.rows[2].cells[3].get_text().strip()) |
フィールドに移動する
move_to_field メソッドを使用すると、ドキュメント内の特定のフィールドに移動できます。さらに、move_to_merge_field メソッドを使用して特定の差し込みフィールドに移動できます。
次のコード例は、ドキュメント ビルダーのカーソルを特定のフィールドに移動する方法を示しています。
# 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 a field using the DocumentBuilder and add a run of text after it. | |
field = builder.insert_field("MERGEFIELD field") | |
builder.write(" Text after the field.") | |
# The builder's cursor is currently at end of the document. | |
self.assertIsNone(builder.current_node) | |
# We can move the builder to a field like this, placing the cursor at immediately after the field. | |
builder.move_to_field(field, True) | |
# Note that the cursor is at a place past the FieldEnd node of the field, meaning that we are not actually inside the field. | |
# If we wish to move the DocumentBuilder to inside a field, | |
# we will need to move it to a field's FieldStart or FieldSeparator node using the DocumentBuilder.move_to() method. | |
self.assertEqual(field.end, builder.current_node.previous_sibling) | |
builder.write(" Text immediately after the field.") |
ヘッダーまたはフッターに移動します
move_to_header_footer メソッドを使用して、ヘッダーまたはフッターの先頭に移動できます。
次のコード例は、ドキュメント ビルダーのカーソルをドキュメントのヘッダーまたはフッターに移動する方法を示しています。
セクションまたは段落に移動する
move_to_paragraph または move_to_section メソッドを使用して、特定のセクションまたは段落に移動できます。さらに、インデックスを指定して、move_to_paragraph メソッド内の段落内の任意の位置または指定した文字にカーソルを移動できます。
次のコード例は、ドキュメント内の特定のセクションおよび特定の段落に移動する方法を示しています。
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
doc.append_child(aw.Section(doc)) | |
# Move a DocumentBuilder to the second section and add text. | |
builder = aw.DocumentBuilder(doc) | |
builder.move_to_section(1) | |
builder.writeln("Text added to the 2nd section.") | |
# Create document with paragraphs. | |
doc = aw.Document(docs_base.my_dir + "Paragraphs.docx") | |
paragraphs = doc.first_section.body.paragraphs | |
self.assertEqual(22, paragraphs.count) | |
# When we create a DocumentBuilder for a document, its cursor is at the very beginning of the document by default, | |
# and any content added by the DocumentBuilder will just be prepended to the document. | |
builder = aw.DocumentBuilder(doc) | |
self.assertEqual(0, paragraphs.index_of(builder.current_paragraph)) | |
# You can move the cursor to any position in a paragraph. | |
builder.move_to_paragraph(2, 10) | |
self.assertEqual(2, paragraphs.index_of(builder.current_paragraph)) | |
builder.writeln("This is a new third paragraph. ") | |
self.assertEqual(3, paragraphs.index_of(builder.current_paragraph)) |