使用文档属性
文档属性允许存储有关文档的一些有用信息。这些属性可以分为两组:
- 包含文档标题、作者姓名、文档统计信息等值的系统或内置。
- 用户定义或自定义,以名称-值对的形式提供,用户可以在其中定义名称和值。
知道有关 API 和版本号的信息直接写入输出文档是很有用的。例如,将文档转换为 PDF 时,Aspose.Words 在"应用程序"字段中填写"Aspose.Words",在"PDF 制作者"字段中填写"Aspose.Words for .NET YY.MN",其中 YY.M.N 是用于转换的 Aspose.Words 版本。有关更多详细信息,请参阅 输出文档中包含的生成者或生产者名称。
访问文档属性
要访问 Aspose.Words 中的文档属性,请使用:
-
built_in_document_properties 获取内置属性。
-
custom_document_properties 获取自定义属性。
built_in_document_properties 和 custom_document_properties 是 DocumentProperty 对象的集合。这些对象可以通过索引器属性按名称或按索引获取。
built_in_document_properties还通过一组返回适当类型值的输入属性提供对文档属性的访问。 custom_document_properties 使您能够在文档中添加或删除文档属性。
DocumentProperty 类允许您获取文档属性的名称、值和类型。 value 返回一个对象,但有一组方法允许您将属性值转换为特定类型。了解属性的类型后,可以使用其中一种 文档属性.to_XXX 方法(例如 文档属性。__str__ 和 DocumentProperty.to_int)来获取适当类型的值。
以下代码示例演示如何枚举文档中的所有内置和自定义属性:
# 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 + "Properties.docx") | |
print("1. Document name: 0", doc.original_file_name) | |
print("2. Built-in Properties") | |
for prop in doc.built_in_document_properties : | |
print("0 : 1", prop.name, prop.value) | |
print("3. Custom Properties") | |
for prop in doc.custom_document_properties : | |
print("0 : 1", prop.name, prop.value) |
在 Microsoft Word 中,您可以使用"文件 → 属性"菜单访问文档属性。

添加或删除文档属性
您无法使用 Aspose.Words 添加或删除内置文档属性。您只能更改或更新它们的值。
要使用 Aspose.Words 添加自定义文档属性,请使用 add 方法,传递新属性名称和适当类型的值。该方法返回新创建的 DocumentProperty 对象。
要删除自定义属性,请使用 remove 方法,向其传递要删除的属性名称,或使用 remove_at 方法按索引删除属性。您还可以使用 clear 方法删除所有属性。
以下代码示例检查文档中是否存在具有给定名称的自定义属性,并添加更多自定义文档属性:
# 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 + "Properties.docx") | |
customDocumentProperties = doc.custom_document_properties | |
if (customDocumentProperties.get_by_name("Authorized") != None) : | |
return | |
customDocumentProperties.add("Authorized", True) | |
customDocumentProperties.add("Authorized By", "John Smith") | |
customDocumentProperties.add("Authorized Date", datetime.today()) | |
customDocumentProperties.add("Authorized Revision", doc.built_in_document_properties.revision_number) | |
customDocumentProperties.add("Authorized Amount", 123.45) |
以下代码示例演示如何删除自定义文档属性:
# 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 + "Properties.docx") | |
doc.custom_document_properties.remove("Authorized Date") |
更新内置文档属性
Aspose.Words 不会像 Microsoft Word 那样自动更新文档属性,但提供了更新某些统计内置文档属性的方法。调用update_word_count方法重新计算并更新以下属性:
创建链接到内容的新自定义属性
Aspose.Words 提供 add_link_to_content 方法来创建链接到内容的新自定义文档属性。该属性返回新创建的属性对象,如果 链接源 无效,则返回 null。
以下代码示例显示如何配置自定义属性的链接:
# 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.start_bookmark("MyBookmark") | |
builder.writeln("Text inside a bookmark.") | |
builder.end_bookmark("MyBookmark") | |
# Retrieve a list of all custom document properties from the file. | |
customProperties = doc.custom_document_properties | |
# Add linked to content property. | |
customProperty = customProperties.add_link_to_content("Bookmark", "MyBookmark") | |
customProperty = customProperties.get_by_name("Bookmark") | |
isLinkedToContent = customProperty.is_link_to_content | |
linkSource = customProperty.link_source | |
customPropertyValue = customProperty.value |
获取文档变量
您可以使用 variables 属性获取文档变量的集合。变量名称和值是字符串。
以下代码示例展示了如何添加和访问文档变量:
# 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 + "Document.docx") | |
doc.variables.add("test", "test") | |
variables = "" | |
for entry in doc.variables : | |
name = entry.key | |
value = entry.value | |
if (variables == "") : | |
variables = "Name: " + name + "," + "Value: " + value | |
else : | |
variables = variables + "Name: " + name + "," + "Value: " + value | |
从文档中删除个人信息
如果您想与其他人共享 Word 文档,您可能需要删除作者姓名和公司等个人信息。为此,请使用 remove_personal_information 属性设置标志,指示 Microsoft Word 将在保存文档时从注释、修订和文档属性中删除所有用户信息。
以下代码示例展示了如何删除个人信息:
# 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 + "Properties.docx") | |
doc.remove_personal_information = True | |
doc.save(docs_base.artifacts_dir + "DocumentPropertiesAndVariables.remove_personal_information.docx") |