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.
Open a file to update
Specify a predicate that selects the metadata properties to add/update
Specify the value you would like to write
Check the actual number of added/updated properties
Save the changes
fromdatetimeimportdatetimefromgroupdocs.metadataimportMetadatafromgroupdocs.metadata.commonimportPropertyValuefromgroupdocs.metadata.taggingimportTagsdefset_metadata_properties():withMetadata("input.vsdx")asmetadata:# The value to write into every matching propertyproperty_value=PropertyValue(datetime.now())# set_properties = add-or-update: the predicate selects the# "created" and "modified" date/time properties across all packagesaffected=metadata.set_properties(lambdap:Tags.time.createdinlist(p.tags)orTags.time.modifiedinlist(p.tags),property_value,)print(f"Properties set: {affected}")# Persist the changes to a new filemetadata.save("output.vsdx")if__name__=="__main__":set_metadata_properties()
input.vsdx is the sample file used in this example. Click here to download it.
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: