Navigation mit Cursor
Während Sie mit einem Dokument arbeiten, auch wenn es kurz oder lang ist, müssen Sie durch Ihr Dokument navigieren. Die Navigation mit einem virtuellen Cursor stellt die Möglichkeit dar, zwischen verschiedenen Knoten in einem Dokument zu navigieren.
Innerhalb eines kurzen Dokuments ist das Bewegen innerhalb eines Dokuments einfach, da Sie die Einfügemarke sogar verschieben können, indem Sie die Pfeiltasten der Tastatur verwenden oder mit der Maus klicken, um die Einfügemarke an die gewünschte Stelle zu setzen. Sobald Sie jedoch ein großes Dokument mit vielen Seiten haben, werden diese grundlegenden Techniken nicht mehr ausreichen.
In diesem Artikel wird erklärt, wie Sie sich in einem Dokument bewegen und mit einem virtuellen Cursor zu verschiedenen Teilen davon navigieren.
Erkennen der aktuellen Cursorposition
Bevor Sie mit der Navigation durch Ihr Dokument beginnen, müssen Sie den aktuell ausgewählten Knoten abrufen. Mithilfe der current_node-Eigenschaft können Sie die genaue Position des Cursors an einem ausgewählten Knoten ermitteln. Anstatt den aktuellen Knoten abzurufen, können Sie außerdem den aktuell ausgewählten Absatz oder den aktuell ausgewählten Abschnitt mithilfe der current_paragraph- und current_section-Eigenschaften abrufen.
Alle Einfügevorgänge, die Sie mit DocumentBuilder ausführen, werden vor current_node eingefügt. Wenn der aktuelle Absatz leer ist oder der Cursor kurz vor dem Ende des Absatzes positioniert ist, gibt der current_node None zurück.
Navigieren durch Methoden in einem Dokument
Wenn Sie Text bearbeiten, ist es wichtig zu wissen, wie Sie in Ihrem Dokument navigieren und wohin Sie sich darin bewegen müssen. Aspose.Words ermöglicht es Ihnen, sich in einem Dokument zu bewegen und zu seinen verschiedenen Abschnitten und Teilen zu navigieren – dies ähnelt der Funktionalität des Navigationsbereichs in Microsoft Word, um zu einer Seite oder Überschrift in einem Word-Dokument zu gelangen, ohne zu scrollen.
Die Hauptmethode besteht darin, die Cursorposition auf einen bestimmten Knoten in Ihrem Dokument zu verschieben. Dies können Sie mithilfe der move_to-Methode erreichen.
Das folgende Codebeispiel zeigt, wie das DocumentBuilder auf verschiedene Knoten in einem Dokument verschoben wird:
# 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) |
Aber neben der grundlegenden move_to-Methode gibt es noch spezifischere.
Navigieren Sie zum Anfang oder Ende eines Dokuments
Sie können mit den Methoden move_to_document_start und move_to_document_end zum Anfang oder Ende Ihres Dokuments springen.
Das folgende Codebeispiel zeigt, wie Sie die Cursorposition an den Anfang oder das Ende eines Dokuments verschieben:
# 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.") |
Navigieren Sie mit Lesezeichen
Sie können einen Ort, den Sie finden möchten, markieren und problemlos wieder dorthin wechseln. Sie können beliebig viele Lesezeichen in Ihr Dokument einfügen und dann durch diese navigieren, indem Sie den Lesezeichen eindeutige Namen geben. Sie können mit der move_to_bookmark-Methode zu einem Lesezeichen wechseln.
Die folgenden Codebeispiele zeigen, wie Sie eine Cursorposition zu einem Lesezeichen verschieben:
# 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]) |
Navigieren Sie zu Tabellenzellen
Mit der move_to_cell-Methode können Sie zu einer Tabellenzelle wechseln. Mit dieser Methode können Sie Ihren Cursor in eine beliebige Zelle einer bestimmten Tabelle navigieren. Darüber hinaus können Sie einen Index angeben, um den Cursor an eine beliebige Position oder ein bestimmtes Zeichen in einer Zelle innerhalb der move_to_cell-Methode zu bewegen.
Das folgende Codebeispiel zeigt, wie eine Cursorposition in eine angegebene Tabellenzelle verschoben wird:
# 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()) |
Navigieren Sie zu einem Feld
Mithilfe der move_to_field-Methode können Sie zu einem bestimmten Feld in Ihrem Dokument wechseln. Darüber hinaus können Sie mithilfe der move_to_merge_field-Methode zu einem bestimmten Zusammenführungsfeld wechseln.
Das folgende Codebeispiel zeigt, wie der Document Builder-Cursor auf ein bestimmtes Feld verschoben wird:
# 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.") |
Navigieren Sie zu einer Kopf- oder Fußzeile
Mit der move_to_header_footer-Methode können Sie zum Anfang einer Kopf- oder Fußzeile wechseln
Das folgende Codebeispiel zeigt, wie der Document Builder-Cursor in die Kopf- oder Fußzeile eines Dokuments verschoben wird:
Navigieren Sie zu einem Abschnitt oder Absatz
Mit den Methoden move_to_paragraph oder move_to_section können Sie zu einem bestimmten Abschnitt oder Absatz wechseln. Darüber hinaus können Sie einen Index angeben, um den Cursor an eine beliebige Position oder ein bestimmtes Zeichen in einem Absatz innerhalb der move_to_paragraph-Methode zu bewegen.
Das folgende Codebeispiel zeigt, wie Sie zu einem bestimmten Abschnitt und einem bestimmten Absatz in einem Dokument wechseln:
# 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)) |