自定义字段属性
Contents
[
Hide
]
Aspose.Words 提供了以编程方式与各种字段属性进行交互的能力。在本文中,我们将查看几个示例,以便您了解使用字段属性的基本原理。您可以在 字段模块 中查看相应类中每种字段类型的属性的完整列表。
字段属性更新
有时用户需要更改字段属性的值。例如,更新 AUTHOR
字段的 author_name 属性或更改 MERGEFIELD
字段的 field_name 属性。
以下代码示例演示如何重命名 Word 文档中的合并字段:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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.insert_field("MERGEFIELD MyMergeField1 \\* MERGEFORMAT") | |
builder.insert_field("MERGEFIELD MyMergeField2 \\* MERGEFORMAT") | |
for f in doc.range.fields: | |
if f.field_type == aw.fields.FieldType.FIELD_MERGE_FIELD: | |
merge_field = f.as_field_merge_field() | |
merge_field.field_name = merge_field.field_name + "_Renamed" | |
merge_field.update() | |
doc.save(ARTIFACTS_DIR + "WorkingWithFields.rename_merge_fields.docx") |
字段显示结果
Aspose.Words 提供了一个属性来获取没有字段分隔符节点的字段的字段结果。我们称之为"假结果"或显示结果; MS Word 通过动态计算字段值在文档中显示它,但文档模型中没有这样的值。
以下代码示例显示了 display_result 属性的用法:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
document = aw.Document(docs_base.my_dir + "Various fields.docx") | |
document.update_fields() | |
for field in document.range.fields : | |
print(field.display_result) |