ניווט עם Cursor
בעת עבודה עם מסמך, גם אם הוא קצר או ארוך, תצטרך לנווט דרך המסמך שלך. ניווט עם קידוד וירטואלי מייצג את היכולת לנווט בין נקודות שונות במסמך.
בתוך מסמך קצר, נע סביב במסמך הוא פשוט כפי שאתה יכול להעביר את נקודת ההכנסה גם על ידי שימוש במפתחי החצים של המקלדת או על ידי לחיצה על העכבר כדי לאתר את נקודת הפתיחה בכל מקום שתרצה. אבל ברגע שיש לך מסמך גדול שיש לו דפים רבים, הטכניקות הבסיסיות האלה לא מספיקות.
מאמר זה מסביר כיצד לנוע סביב במסמך לנווט עם cursor וירטואלי לחלקים שונים ממנו.
המונחים: Current Cursor
לפני שתתחיל בתהליך של ניווט דרך המסמך שלך, תצטרך לקבל את הצומת שנבחר כרגע. אתה יכול לקבל את המיקום המדויק של cursor בצומת נבחר על ידי שימוש current_node רכוש. בנוסף, במקום לקבל את הצומת הנוכחי, אתה יכול לקבל את הסעיף שנבחר כרגע או את החלק שנבחר על ידי שימוש בסעיף שנבחר כיום על ידי שימוש current_paragraph ו current_section תכונות.
כל פעולות כניסה שאתה מבצע באמצעות DocumentBuilder יוכנס לפני current_node. כאשר הסעיף הנוכחי ריק או הcursor ממוקם ממש לפני סוף הסעיף, הסעיף. current_node לא חוזר אף אחד.
שיטות ניווט במסמך
כאשר אתה עורך טקסט, חשוב לדעת כיצד לנווט את המסמך שלך והיכן בדיוק לעבור בו. Aspose.Words מאפשר לך לנוע סביב במסמך לנווט לחלקים השונים שלה וחלקים - זה דומה לפונקציונליות של הפנינה הניווט ב Microsoft Word ללכת לדף או לכותרת במסמך Word מבלי לגלול.
השיטה העיקרית היא להיות מסוגל להעביר את מיקום cursor לצומת מסוים במסמך שלך, אתה יכול להשיג את זה על ידי שימוש 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 שיטות.
הדוגמה הבאה של הקוד מראה כיצד להעביר את מיקום הcursor להתחלה או לסוף המסמך:
# 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 שיטה.
דוגמאות הקוד הבאות מראות כיצד להעביר מיקום cursor לסימן ספר:
# 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 שיטה. שיטה זו תאפשר לך לנווט את הcursor לתוך כל תא בטבלה מסוימת. בנוסף, אתה יכול לציין אינדקס כדי להעביר את הcursor לכל עמדה או אופי שצוין בתא בתוך התא בתוך התא. move_to_cell שיטה.
דוגמה הקוד הבא מראה כיצד להעביר מיקום cursor לתא שולחן מוגדר:
# 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 שיטה
הדוגמה הבאה של הקוד מראה כיצד להעביר את בונה המסמך cursor אל מנהל מסמך או רגל:
ניווט לסעיף או לפסקה
אתה יכול לעבור לסעיף מסוים או לפסקה על ידי שימוש move_to_paragraph או move_to_section שיטות. בנוסף, באפשרותך לציין אינדקס כדי להעביר את הcursor לכל עמדה או דמות מוגדרת בפסקה בתוך הסעיף בתוך הסעיף. 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)) |