I'm trying to automate updates to the dynamic date in a Pro 3.3.2 map document. The dynamic date is in a text element named PrintDate. I can access the element, but not the date. Things I've tried:
for lyt in aprx.listLayouts():
for elm in lyt.listElements('TEXT_ELEMENT'):
if elm.text.endswith("2024"):
print(f'{elm.name} | {elm.text}') #prints out the one other element in the document with 2024, not the dynamic date
elif elm.name == "PrintDate":
print(f'{elm.name} | {elm.text}') #prints out the whole text element with the dynamic date format
I'm not after making the date un-dynamic as there are dozens of maps with this set-up. Anyone have an idea?
Solved! Go to Solution.
try this, The datetime.now().strftime("%B %d, %Y") part formats the current date. The element text is set to include the dynamic date tag <dyn type="date" format="MMMM d, yyyy"/>
import arcpy
from datetime import datetime
# Access the ArcGIS Pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Get the current date in the desired format
current_date = datetime.now().strftime("%B %d, %Y")
# Loop through the layouts and text elements
for lyt in aprx.listLayouts():
for elm in lyt.listElements('TEXT_ELEMENT'):
if elm.name == "PrintDate":
# Update the text element with the dynamic date
elm.text = f"Print Date: <dyn type=\"date\" format=\"MMMM d, yyyy\"/>"
#elm.text = '<dyn type="date" format="YYYY-MM-DD"/>'
print(f'Updated {elm.name} to {elm.text}')
try this, The datetime.now().strftime("%B %d, %Y") part formats the current date. The element text is set to include the dynamic date tag <dyn type="date" format="MMMM d, yyyy"/>
import arcpy
from datetime import datetime
# Access the ArcGIS Pro project
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Get the current date in the desired format
current_date = datetime.now().strftime("%B %d, %Y")
# Loop through the layouts and text elements
for lyt in aprx.listLayouts():
for elm in lyt.listElements('TEXT_ELEMENT'):
if elm.name == "PrintDate":
# Update the text element with the dynamic date
elm.text = f"Print Date: <dyn type=\"date\" format=\"MMMM d, yyyy\"/>"
#elm.text = '<dyn type="date" format="YYYY-MM-DD"/>'
print(f'Updated {elm.name} to {elm.text}')