用光标导航
在处理文档时,无论文档长短,您都需要浏览文档。使用虚拟光标导航表示在文档中的不同节点之间导航的能力。
在简短的文档中,在文档中移动非常简单,因为您甚至可以使用键盘的箭头键或单击鼠标将插入点定位到所需的位置,从而移动插入点。但是,一旦您有一个包含很多页的大型文档,这些基本技术就不够了。
本文介绍如何在文档中移动并使用虚拟光标导航到文档的不同部分。
检测当前光标位置
在开始浏览文档之前,您需要获取当前选定的节点。您可以使用 current_node 属性获取光标在选定节点处的准确位置。此外,您可以使用 current_paragraph 和 current_section 属性获取当前选定的段落或当前选定的部分,而不是获取当前节点。
您使用 DocumentBuilder 执行的任何插入操作都将插入到 current_node 之前。当当前段落为空或光标位于段落末尾之前时,current_node 返回 None。
在文档中导航方法
编辑文本时,了解如何导航文档以及在文档中准确移动的位置非常重要。 Aspose.Words 允许您在文档中移动并导航到其不同的部分和部分 - 这类似于 Microsoft Word 中导航窗格的功能,无需滚动即可转到 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)) |