使用 Python 管理演示文稿属性
介绍
Aspose.Slides for Python via .NET 支持 内置 和 自定义 文档属性。使用这些属性读取和更新演示文稿元数据,例如作者、标题和用户定义的值。
Aspose.Slides 允许您通过 DocumentProperties 类处理演示文稿文档属性。该类的实例由 Presentation.document_properties 返回。以下示例展示了如何读取、修改和管理这些属性。
Note
Aspose.Slides 在保存演示文稿时管理应用程序和生成者的元数据;这些字段用于标识创建输出的软件。管理演示文稿属性
文档属性在演示文稿内容旁存储描述性信息。它们分为两类:
- 内置属性 包含标准元数据,例如标题、作者和文档统计信息。
- 自定义属性 存储用户定义的名称/值对。
Aspose.Slides for Python via .NET 提供对两类属性的访问。在 PowerPoint 中,选择 文件 → 信息 → 属性 → 高级属性 以检查文档元数据。对话框将信息分组到 摘要、统计信息、内容 和 自定义 等选项卡。使用 自定义 选项卡管理用户定义的属性。
从加密演示文稿读取公共属性
打开密码通常同时保护演示文稿内容和文档属性。当使用 ProtectionManager.encrypt_document_properties 并将其设置为 False 加密演示文稿时,其文档属性保持为公共。此时应用程序可以将 LoadOptions.only_load_document_properties 设置为 True,在不提供打开密码的情况下读取公共元数据。
LoadOptions.only_load_document_properties 控制 Aspose.Slides 加载的内容;它不进行任何解密。如果属性已包含在加密中,则在没有密码的情况下加载会失败。如果演示文稿未加密,则此选项被忽略,完整演示文稿将被加载。
该示例需要一个文档属性保持公共的加密演示文稿。它通过 ProtectionManager.is_only_document_properties_loaded 验证加载模式,然后通过 Presentation.document_properties 读取内置属性:
import aspose.slides as slides
load_options = slides.LoadOptions()
load_options.only_load_document_properties = True
with slides.Presentation("public-properties-encrypted.pptx", load_options) as presentation:
if presentation.protection_manager.is_only_document_properties_loaded:
document_properties = presentation.document_properties
print("Author: " + document_properties.author)
print("Title: " + document_properties.title)
print("Keywords: " + document_properties.keywords)
else:
print("The presentation was not loaded in document-properties-only mode.")
在此模式下,幻灯片内容不会被加载。幻灯片、母版、版式、形状、媒体以及其他演示文稿对象均不可用。应用程序在执行需要完整演示文稿对象模型的操作之前,应始终检查 ProtectionManager.is_only_document_properties_loaded。
Security
公共元数据可能泄露作者姓名、标题、主题、关键字、公司信息、注释和自定义值。请将敏感属性与演示文稿一起加密。仅在索引、分类、搜索或文档管理系统有特定需求必须在不使用密码的情况下访问时,才将其保持公共。更新加密演示文稿的属性
对于加密的 PPTX 文件,使用 LoadOptions.only_load_document_properties 加载的演示文稿仅用于读取公共元数据。Aspose.Slides 无法从仅包含元数据的对象保存已更改的属性,因为公共属性必须与加密演示文稿中的相应数据保持一致。因此,更新这些属性需要正确的打开密码并完整加载演示文稿。
该示例需要一个具有公共文档属性且已知打开密码的加密 PPTX 演示文稿。它使用 LoadOptions.password 打开完整演示文稿,更新公共内置属性并保存结果。随后使用 PresentationInfo.is_encrypted 验证加密仍然保留,并在不使用密码的情况下重新打开公共元数据以验证新值:
import aspose.slides as slides
input_path = "public-properties-encrypted.pptx"
output_path = "updated-public-properties-encrypted.pptx"
load_options = slides.LoadOptions()
load_options.password = "open_password"
with slides.Presentation(input_path, load_options) as presentation:
document_properties = presentation.document_properties
document_properties.title = "Updated Product Roadmap"
document_properties.keywords = "roadmap, planning, indexed"
presentation.save(output_path, slides.export.SaveFormat.PPTX)
presentation_info = slides.PresentationFactory.instance.get_presentation_info(output_path)
print("The presentation is encrypted: " + str(presentation_info.is_encrypted))
metadata_load_options = slides.LoadOptions()
metadata_load_options.only_load_document_properties = True
with slides.Presentation(output_path, metadata_load_options) as metadata_presentation:
if metadata_presentation.protection_manager.is_only_document_properties_loaded:
print("Title: " + metadata_presentation.document_properties.title)
print("Keywords: " + metadata_presentation.document_properties.keywords)
else:
print("The presentation was not loaded in document-properties-only mode.")
如果应用程序不被允许解密或加载演示文稿内容,则必须将加密 PPTX 文件的公共属性视为只读。
访问内置属性
DocumentProperties 类通过强类型属性公开内置元数据,包括 DocumentProperties.author、DocumentProperties.title 和 DocumentProperties.created_time。本示例打开现有演示文稿并将其存储的元数据打印到控制台。值取决于输入的演示文稿;某些字段可能为空。
import aspose.slides as slides
with slides.Presentation("AccessBuiltInProperties.pptx") as presentation:
document_properties = presentation.document_properties
print("Category:", document_properties.category)
print("Content status:", document_properties.content_status)
print("Creation date:", document_properties.created_time)
print("Author:", document_properties.author)
print("Comments:", document_properties.comments)
print("Keywords:", document_properties.keywords)
print("Last modified by:", document_properties.last_saved_by)
print("Manager:", document_properties.manager)
print("Modified date:", document_properties.last_saved_time)
print("Presentation format:", document_properties.presentation_format)
print("Last print date:", document_properties.last_printed)
print("Is shared between producers:", document_properties.shared_doc)
print("Subject:", document_properties.subject)
print("Title:", document_properties.title)
报告修订号和编辑时间
要报告 presentations 文件夹中每个 PPTX 文件的修订号和总编辑时间,请使用 PresentationInfo.read_document_properties 获取其 DocumentProperties,而无需加载幻灯片进行编辑。Python 将持续时间显示为 timedelta,当记录的时间达到 24 小时时会包括天数。
from pathlib import Path
import aspose.slides as slides
print("File | RevisionNumber | TotalEditingTime")
for path in Path("presentations").glob("*.pptx"):
if path.is_file():
info = slides.PresentationFactory.instance.get_presentation_info(str(path))
properties = info.read_document_properties()
print(f"{path.name} | {properties.revision_number} | {properties.total_editing_time}")
对于记录了 1 天 2 小时 5 分钟编辑时间的演示文稿,报告将包含如下行。
File | RevisionNumber | TotalEditingTime
long.pptx | 17 | 1 day, 2:05:00
DocumentProperties.revision_number 是可写的元数据。仅凭其值无法确定演示文稿保存了多少次,也无法重建其版本历史。
DocumentProperties.total_editing_time 报告文件中存储的编辑时长。值为 0:00:00 表示记录的时长为零,即使有人编辑了演示文稿。
修改内置属性
要更新内置元数据,请为 Presentation.document_properties 公开的可写属性赋值。本示例打开现有演示文稿,设置 DocumentProperties.author、DocumentProperties.title、DocumentProperties.subject、DocumentProperties.comments 和 DocumentProperties.manager,然后使用 Presentation.save 保存更新后的演示文稿。保存的 PPTX 包含新的元数据。
import aspose.slides as slides
with slides.Presentation("ModifyBuiltInProperties.pptx") as presentation:
document_properties = presentation.document_properties
document_properties.author = "Aspose.Slides for Python via .NET"
document_properties.title = "Manage PowerPoint Presentation Properties"
document_properties.subject = "Modify Built-in Properties"
document_properties.comments = "Aspose description"
document_properties.manager = "Aspose manager"
presentation.save("DocumentProperties_output.pptx", slides.export.SaveFormat.PPTX)
管理超链接基址
DocumentProperties.hyperlink_base 保存文档级别的相对超链接基址。当将演示文稿及其链接文件移动到其他位置时,这很有用。它与单个超链接的 Hyperlink.external_url 是分离的:更改基址不会重新写入存储的链接地址或更改绝对目标。
该示例需要一个包含相对链接(如 target.html)和指向文件或网站的绝对链接的演示文稿。它读取现有基址,分配一个新的基址,然后保存并重新打开演示文稿以检查该值。随后使用 "" 清除基址,并在另一个保存的文件中验证空值。请将示例基址替换为您的链接文件所在位置。
import aspose.slides as slides
with slides.Presentation("hyperlinks.pptx") as presentation:
print("Original base:", presentation.document_properties.hyperlink_base)
new_base = "https://example.com/new/"
presentation.document_properties.hyperlink_base = new_base
presentation.save("hyperlinks-updated.pptx", slides.export.SaveFormat.PPTX)
with slides.Presentation("hyperlinks-updated.pptx") as updated_presentation:
print("Base preserved:", updated_presentation.document_properties.hyperlink_base == new_base)
updated_presentation.document_properties.hyperlink_base = ""
updated_presentation.save("hyperlinks-cleared.pptx", slides.export.SaveFormat.PPTX)
with slides.Presentation("hyperlinks-cleared.pptx") as cleared_presentation:
print("Base cleared:", cleared_presentation.document_properties.hyperlink_base == "")
两个验证消息应打印 True。清除基址后,单个超链接地址保持不变。
当基址指向 old 文件夹时,指向 target.pptx 的相对链接会打开 old/target.pptx。将基址更改为 new 文件夹后,保存并重新打开演示文稿,同一链接会打开 new/target.pptx。绝对链接仍然打开相同的文件。请在 PowerPoint 中在移动演示文稿或更改基址前后测试您的相对链接。
添加自定义演示文稿属性
自定义属性存储没有对应内置字段的元数据。DocumentProperties.set_custom_property_value 方法允许您按属性名称分配值。本示例创建一个演示文稿,添加两个文本值和一个整数版本值,并使用 Presentation.save 保存演示文稿。为已存在的名称分配值会替换该属性的值。
import aspose.slides as slides
with slides.Presentation() as presentation:
document_properties = presentation.document_properties
document_properties.set_custom_property_value("Reviewed by", "John Smith")
document_properties.set_custom_property_value("Confidentiality level", "Internal")
document_properties.set_custom_property_value("Document version", 2)
presentation.save("CustomDocumentProperties_output.pptx", slides.export.SaveFormat.PPTX)
访问并修改自定义属性
本示例打开包含自定义属性的演示文稿,并使用 DocumentProperties.count_of_custom_properties 对其进行遍历。DocumentProperties.get_custom_property_name 接受零基索引并返回用于 DocumentProperties.set_custom_property_value 的名称。示例需要自定义属性全部为字符串的演示文稿。它打印每个原始名称和值,将每个值替换为带编号的字符串,并保存更改。如果没有自定义属性,循环将不做任何更改。
import aspose.slides as slides
with slides.Presentation("AccessAndModifyProperties.pptx") as presentation:
document_properties = presentation.document_properties
for i in range(document_properties.count_of_custom_properties):
property_name = document_properties.get_custom_property_name(i)
property_value = [""]
document_properties.get_custom_property_value(property_name, property_value)
print("Custom property name:", property_name)
print("Custom property value:", property_value[0])
document_properties.set_custom_property_value(property_name, "New Value " + str(i + 1))
presentation.save("CustomProperties_output.pptx", slides.export.SaveFormat.PPTX)
DocumentProperties.get_custom_property_value 通过其第二个参数传入的一元素列表返回值。占位符必须匹配存储属性的类型:对字符串使用 [""],对整数使用 [0]。类型不匹配会引发 InvalidCastException;请根据自定义属性的模式选择占位符。
实时示例
尝试在线应用程序 查看并编辑 PowerPoint 元数据 以了解如何使用 Aspose.Slides API 操作文档属性:
常见问题
如何从演示文稿中删除内置属性?
内置属性是演示文稿的组成部分,不能完全删除。不过,您可以更改其值,或者在相应属性允许的情况下将其设为空。
如果我添加的自定义属性已经存在,会怎样?
如果添加的自定义属性已经存在,其原有值将被新值覆盖。您无需事先删除或检查该属性,Aspose.Slides 会自动更新属性值。
是否可以在不完全加载演示文稿的情况下访问演示文稿属性?
可以。使用 PresentationFactory.get_presentation_info 然后 PresentationInfo.read_document_properties 可在不创建 Presentation 实例的情况下读取存储的文档元数据。完整的报告示例和特定格式的限制请参见 构建轻量级演示文稿清单。
是否可以在无需打开密码的情况下读取加密演示文稿的公共属性?
可以。演示文稿必须使用 ProtectionManager.encrypt_document_properties 并将其设置为 False 加密,并且加载时必须将 LoadOptions.only_load_document_properties 设置为 True。
是否可以在仅文档属性模式下更新加密的 PPTX 文件?
不能。公共属性和加密属性的数据必须保持一致,因此要更新加密的 PPTX 文件,需要使用正确的打开密码完整加载演示文稿。
