Select to view content in your preferred language

Automate metadata to update summary section

66
2
7 hours ago
AndreaB_
Frequent Contributor

Hello everyone,

I am not a developer, I'm using ArcGIS Pro 3.5.7. I am using ModelBuilder to run a scheduled model every 1st day of the month to export feature classes from a SQL geodatabase to a file geodatabase. The next script I would like to run after hours (every 1st of the month) is to update the summary section of the metadata of the feature classes in the file geodatabase with the current month and year and another 2 sentences added to the end of the current summary section. 

I have asked my friend Google AI to tell me how to do this. It has failed. So I come to you, much smarter people. Here is what Google AI told me and I tried to run this in a ArcGIS Pro notebook.

 

from datetime import datetime
import arcpy

dataset_path = r"E:\ArcGIS Pro\Name.gdb\Name"

try:
    # 1. Generate text snippet safely using built-in formatting
    append_text = datetime.now().strftime(", %B %Y. Please note that")
    
    # 2. Access the metadata object
    target_metadata = arcpy.metadata.Metadata(dataset_path)
    
    # 3. Safely read current summary using a rigid string conversion check
    raw_summary = target_metadata.summary
    
    # Check if raw_summary is a valid string, not a class type or None
    if isinstance(raw_summary, str) and not isinstance(raw_summary, type):
        current_summary = raw_summary
    else:
        current_summary = ""
        
    # 4. Append and write back
    target_metadata.summary = f"{current_summary}{append_text}"
    
    # 5. Save changes
    target_metadata.save()
    print(f"Successfully updated metadata summary.")

except Exception as e:
    print(f"Failed to update metadata: {e}")

I get:

Failed to update metadata: <class 'type'> returned a result with an exception set

 I have tried to find documentation on this and I searched the community for previous questions and answers but those are mostly very old so the software version is too old, and in some cases, they were using good old ArcMap. I did watch a few Esri videos about Metadata, very informative, but no mention of using python.

https://community.esri.com/t5/arcgis-pro-blog/live-training-seminar-metadata-essentials-for-ai/ba-p/... 

https://mediaspace.esri.com/media/t/1_o8wuj2ba 

https://doc.esri.com/en/arcgis-pro/latest/arcpy/metadata/what-is-the-metadata-module.html

Is this possible? Please help, thank you!

A

 

 

0 Kudos
2 Replies
BobBooth1
Esri Regular Contributor

This worked for me:

import arcpy
from arcpy import metadata as md
from datetime import datetime

# Define the path to your dataset
item_path = r"C:\Users\MyUserName\Documents\ArcGIS\Projects\MetadataTest\MetadataTest.gdb\OH_geol_poly"

append_text = datetime.now().strftime(", %B %Y. Please note that")
other_text = "Some arbitary other text."

# Access the metadata
item_md = md.Metadata(item_path)
oldSummary = item_md.summary
try:
    item_md.summary = oldSummary + "\r" + append_text + "\r" + other_text
    item_md.save()
except:
    print("Ope! Could not write to metadata. Maybe locked?")
item_md.summary    

 

metadata_update_code.png

 

0 Kudos
BobBooth1
Esri Regular Contributor

This tutorial shows how you can schedule a Python script to run at specific times.

https://learn.arcgis.com/en/projects/schedule-automated-near-real-time-data-updates/

0 Kudos