Select to view content in your preferred language

How to access dynamic text with arcpy

395
1
Jump to solution
10-29-2024 11:45 AM
JaredPilbeam2
MVP Alum

JaredPilbeam2_0-1730227010773.png

 

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?

 

1 Solution

Accepted Solutions
TonyAlmeida
MVP Regular Contributor

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}')

 

 

View solution in original post

0 Kudos
1 Reply
TonyAlmeida
MVP Regular Contributor

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}')

 

 

0 Kudos