Managing document properties in PowerPoint is an essential aspect of presentation creation. These properties serve as metadata that provides important information about the file, such as the author, subject, and keywords. By being able to add, retrieve, or remove document properties, users gain control over the organization and customization of their presentations. Whether it's adding relevant tags for easy categorization, accessing authorship details, or removing sensitive data, effectively managing document properties in PowerPoint ensures seamless collaboration and professionalism in your slide decks.
In this article, you will learn how to add, read, and remove document properties in a PowerPoint file in Python by using the Spire.Presentation for Python library.
- Add Document Properties to a PowerPoint File
- Read Document Properties of a PowerPoint File
- Remove Document Properties from a PowerPoint File
Install Spire.Presentation for Python
This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip commands.
pip install Spire.Presentation
If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows
Prerequisite Knowledge
Document properties can be divided into two types: standard document properties and custom document properties.
- Standard document properties are pre-defined properties that are commonly used across various PowerPoint presentations. Some examples of standard document properties include title, author, subject, keywords and company. Standard document properties are useful for providing general information and metadata about the presentation.
- Custom document properties are user-defined properties that allow you to add specific information to a PowerPoint presentation. Unlike standard document properties, custom properties are not predefined and can be tailored to suit your specific needs. Custom properties usually provide information relevant to your presentation that may not be covered by the default properties.
Spire.Presentation for Python offers the DocumentProperty class to work with both standard document properties and custom document properties. The standard document properties can be accessed using the properties like Title, Subject, Author, Manager, Company, etc. of the DocumentProperty class. To add or retrieve custom properties, you can use the set_Item() method and the GetPropertyName() method of the DocumentProperty class.
Add Document Properties to a PowerPoint File in Python
To add or change the standard document properties, you can assign values to the DocumentProperty.Title proerpty, DocumentProperty.Subject property and other similar properties. To add custom properties to a presentation, use the DocumentProperty.set_Item(name: str, value: SpireObject) method. The detailed steps are as follows.
- Create a Presentation object.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get the DocumentProperty object.
- Add standard document properties to the presentation by assigning values to the Title, Subject, Author, Manager, Company and Keywords properties of the object.
- Add custom properties to the presentation using set_Item() of the object.
- Save the presentation to a PPTX file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\input.pptx") # Get the DocumentProperty object documentProperty = presentation.DocumentProperty # Set built-in document properties documentProperty.Title = "Annual Sales Presentation" documentProperty.Subject = "Company performance and sales strategy" documentProperty.Author = "John Smith" documentProperty.Manager = "Sarah Johnson" documentProperty.Company = "E-iceblue Corporation" documentProperty.Category = "Business" documentProperty.Keywords = "sales, strategy, performance" documentProperty.Comments = "Please review and provide feedback by Friday" # Add custom document properties documentProperty.set_Item("Document ID", Int32(12)) documentProperty.set_Item("Authorized by", String("Product Manager")) documentProperty.set_Item("Authorized Date", DateTime(2024, 1, 10, 0, 0, 0, 0)) # Save to file presentation.SaveToFile("output/Properties.pptx", FileFormat.Pptx2019) presentation.Dispose()
Read Document Properties of a PowerPoint File in Python
The DocumentProperty.Title and the similar properties are not only used to set standard properties but can return the values of standard properties as well. Since the name of a custom property is not constant, we need to get the name using the DocumentProperty.GetPropertyName(index: int) method. And then, we're able to get the property's value using the DocumentProperty.get_Item(name: str) method.
The steps to read document properties of a PowerPoint file are as follows.
- Create a Presentation object.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get the DocumentProperty object.
- Get the standard document properties by using the Title, Subject, Author, Manager, Company, and Keywords properties of the object.
- Get the count of the custom properties, and iterate through the custom properties.
- Get the name of a specific custom property by its index using DocumentProperty.GetPropertyName() method.
- Get the value of the property using DocumentProperty.get_Item() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Properties.pptx") # Get the DocumentProperty object documentProperty = presentation.DocumentProperty # Get the built-in document properties print("Title: " + documentProperty.Title) print("Subject: " + documentProperty.Subject) print("Author: " + documentProperty.Author) print("Manager : " + documentProperty.Manager) print("Company: " + documentProperty.Company) print("Category: " + documentProperty.Category) print("Keywords: " + documentProperty.Keywords) print("Comments: " + documentProperty.Comments) # Get the count of the custom document properties count = documentProperty.Count # Iterate through the custom properties for i in range(count): # Get the name of a specific custom property customPropertyName = documentProperty.GetPropertyName(i) # Get the value of the custom property customPropertyValue = documentProperty.get_Item(customPropertyName) # Print the result print(customPropertyName + ": " + str(customPropertyValue))
Remove Document Properties from a PowerPoint File in Python
Removing a standard property means assigning an empty string to a property like DocumentProperty.Title. To remove the custom properties, Spire.Presentation provides the DocumentProperty.Remove(name: str) method. The following are the steps to remove document properties from a PowerPoint file in Python.
- Create a Presentation object.
- Load a PowerPoint document using Presentation.LoadFromFile() method.
- Get the DocumentProperty object.
- Set the Title, Subject, Author, Manager, Company, and Keywords properties of the object to empty strings.
- Get the count of the custom properties.
- Get the name of a specific custom property by its index using DocumentProperty.GetPropertyName() method.
- Remove the custom property using DocumentProperty.Remove() method.
- Save the presentation to a PPTX file using Presentation.SaveToFile() method.
- Python
from spire.presentation.common import * from spire.presentation import * # Create a Presentation object presentation = Presentation() # Load a PowerPoint document presentation.LoadFromFile("C:\\Users\\Administrator\\Desktop\\Properties.pptx") # Get the DocumentProperty object documentProperty = presentation.DocumentProperty # Set built-in document properties to empty strings documentProperty.Title = "" documentProperty.Subject = "" documentProperty.Author = "" documentProperty.Manager = "" documentProperty.Company = "" documentProperty.Category = "" documentProperty.Keywords = "" documentProperty.Comments = "" # Get the count of the custom document properties i = documentProperty.Count while i > 0: # Get the name of a specific custom property customPropertyName = documentProperty.GetPropertyName(i - 1) # Remove the custom property documentProperty.Remove(customPropertyName) i = i - 1 # Save the presentation to a different pptx file presentation.SaveToFile("Output/RemoveProperties.pptx",FileFormat.Pptx2019)
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.