Converting Dynamic Text to Text Using Python

2166
8
07-29-2020 09:31 AM
WilliamSmith6
New Contributor II

Hello,

I have a few bookmarks are I am cycling through to export to PDF. I am using ArcGIS Pro 2.6's ArcGIS Notebooks. I am able to define the correct bookmarks and export to PDF, but when I try to make a custom name using the Dynamic Text that is present in the layout, I run into issues.

I have multiple a test site that has multiple sites within it. Let's say I have Test Site 1 and within Test Site 1 I have Sample Site A, Sample Site B, and Sample Site C. I have 2 bookmarks, 1 with Sample Site A & B visible, and the other with just Sample Site C visible.

This text is updated in my layout based on what sites are in the viewing extent of the map tab using the Attribute Dynamic Text option. So when I pan in the map, the text with change.

The problem is, I cannot get my python module to recognize this text as actual text.

I want to use the Test Site Name (Test Site 1) and whichever Sample Sites are visible in the map to build the name of my output file; essentially just concatenating the 2 strings and using it for the file name.

The "Location Name" is the name of the Map Element for Test Site 1. "Visible Test Sites" is the Map Element name for the Sample Sites.

Here is my code:

for bkmk in bkmks:
    mf.zoomToBookmark(bkmk)
    lns = lyt.listElements("TEXT_ELEMENT", "Location Name")
    tst = lyt.listElements("TEXT_ELEMENT", "Visible Test Sites")
    fname = lns + tst
    lyt.exportToPDF(os.path.join(r"U:\Drones\Operations\BeneficialUseSites_Processing", str(fname)+".pdf"))

However, in the error it looks like it is using the Dynamic Text expression instead of the text itself in the file name. See attached screenshot.

Any ideas?

0 Kudos
8 Replies
RockyRudolph
New Contributor II

Try this instead and see if it works. I believe the listElements function returns a list of layer objects, so you need to note that it's the first (zero index) thing you want to return.  Then you need to call the text property to get the actual text.

for bkmk in bkmks:
    mf.zoomToBookmark(bkmk)
    lns = lyt.listElements("TEXT_ELEMENT", "Location Name")[0]
    tst = lyt.listElements("TEXT_ELEMENT", "Visible Test Sites")[0]
    fname = lns.text + tst.text
    lyt.exportToPDF(os.path.join(r"U:\Drones\Operations\BeneficialUseSites_Processing", fname +".pdf"))

0 Kudos
WilliamSmith6
New Contributor II

I had something similar in a previous version of my script. It always crashed ArcGIS Pro, for some reason. I just edited my code to incorporate your suggestion and I got a box that says "gathering information about the error..." Then it brought up the ArcGIS Application to send an error report to ESRI. This occurs every time I try to run the script. I've tried a few times now.

0 Kudos
RockyRudolph
New Contributor II

Other things to try.

Liberally lace your script with print statements and see if those execute. If they run but then fail at some point, that will help narrow down what is generating the error or locking up Pro.

print("Entering for loop")
for bkmk in bkmks:
print("Getting bookmarks")
mf.zoomToBookmark(bkmk)
print("Getting list elements")
lns = lyt.listElements("TEXT_ELEMENT", "Location Name")[0]
tst = lyt.listElements("TEXT_ELEMENT", "Visible Test Sites")[0]
fname = lns.text + tst.text
print("Exporting to pdf")
lyt.exportToPDF(os.path.join(r"U:\Drones\Operations\BeneficialUseSites_Processing", fname +".pdf"))

Try running the script outside of arc pro entirely.  You may need to site the full path of the aprx file and do some other adjustments to the script to make this possible.  Errors or crashing still?

0 Kudos
WilliamSmith6
New Contributor II

It crashes when it gets to  ExporttoPDF. I put some more print statements and found the attached. Even with .text it was still printing the full expression, not the text. I thought just getting rid of the ":" would be a problem, but it's more than that.

0 Kudos
RockyRudolph
New Contributor II

Ok now we're getting closer. This is a little hard to do without seeing your entire project and data but I'll keep going. Why do you need to access that dynamic text through python?  What is it in there as the map changes to each bookmark that you need to extract?  Site name?  Are you only needing to extract that site name so you can save it by the name to a pdf with the same name?  Can you achieve what you're trying to do with Map Series instead of python?  It might be easier to do a Map Series with a featureclass delineating your bookmarks so you can access fields within that data and then access that data with the python script. 

From that last screenshot it looks like python is getting the text from that element, but the text is that big string of code that actually makes the dynamic text display dynamic stuff on the map in Pro.  So it seems useless as text within python since it contains all those crazy characters and you wouldn't want to use that as a file name for you pdf anyway. 

0 Kudos
WilliamSmith6
New Contributor II

Basically I have Test Site and Sample Site locations that can change depending on the bookmark that I select because they are Dynamic Text. I wanted to see if I could take that text and use it to create a unique file name depending on that information.  The text from the Dynamic Test keep printing the expression. It is trying to use that for the file name and then ends up crashing.   I may need to find a different way. Thanks for the help!

0 Kudos
RockyRudolph
New Contributor II

Ok got it now. I'm stumped. Maybe someone else knows if there's a way to extract the value of a dynamic text element. But I'm guessing it only renders onto the screen inside Pro.

A less desirable outcome probably would be to just use bkmk.name to save the pdf.  Or to use map series and to query the features inside each map series view to get the site names, which sounds like a lot of work. Good luck

0 Kudos
WilliamSmith6
New Contributor II

The easiest for now is to name the bookmarks how I want to name the file, and then iterate.

The code was originally posted on ESRI's webpage for Bookmarks. Bookmark Sample 1: https://pro.arcgis.com/en/pro-app/arcpy/mapping/bookmark-class.htm 


This works, but I just have to be careful how I name the bookmarks.

0 Kudos