Set metadata properties

Update or add metadata properties satisfying a predicate

The set_properties method combines two operations: add and update. If an existing property satisfies the specified predicate, its value is updated. If a known property that satisfies the predicate is missing from a metadata package, it is added to the appropriate package.

The code snippet below demonstrates a basic usage scenario of the set_properties method.

  1. Open a file to update
  2. Specify a predicate that selects the metadata properties to add/update
  3. Specify the value you would like to write
  4. Check the actual number of added/updated properties
  5. Save the changes
from datetime import datetime

from groupdocs.metadata import Metadata
from groupdocs.metadata.common import PropertyValue
from groupdocs.metadata.tagging import Tags


def set_metadata_properties():
    with Metadata("input.vsdx") as metadata:
        # The value to write into every matching property
        property_value = PropertyValue(datetime.now())
        # set_properties = add-or-update: the predicate selects the
        # "created" and "modified" date/time properties across all packages
        affected = metadata.set_properties(
            lambda p: Tags.time.created in list(p.tags)
            or Tags.time.modified in list(p.tags),
            property_value,
        )
        print(f"Properties set: {affected}")
        # Persist the changes to a new file
        metadata.save("output.vsdx")


if __name__ == "__main__":
    set_metadata_properties()

input.vsdx is the sample file used in this example. Click here to download it.

Binary file (VSDX, 35 KB)

Download full output

As a result, we update all existing metadata properties that hold the date the document was created/updated. If a metadata package doesn’t contain such properties but they belong in its structure, they are added.

For more information on searching metadata, please refer to the following articles:

More resources

Advanced usage topics

To learn more about library features and get familiar how to manage metadata and more, please refer to the advanced usage section.

GitHub examples

You may easily run the code above and see the feature in action in our GitHub examples:

Free online document metadata management App

You are welcome to view and edit metadata of PDF, DOC, DOCX, PPT, PPTX, XLS, XLSX, emails, images and more with our free online Free Online Document Metadata Viewing and Editing App.