AnsweredAssumed Answered

TEXT_ELEMENT content in map layout not displaying correctly

Question asked by jpilbeam on Dec 6, 2018
Latest reply on Dec 10, 2018 by jpilbeam

Using ArcMap 10.5.1 and Python 2.7, I have a script that finds a text box in map layout of an MXD(s) by two key words "PRINT DATE" and changes the text to new text. I believe the script is doing what I want it to and it runs with no error. However, the resulting text in the text box is illegible. 

 

Script:

import arcpy, os
from arcpy import env

arcpy.env.workspace = ws = r"path\to\TestFolder"

# format new text
txtBlock = [
'Prepared by the Will County GIS Division',
'302 N Chicago St. Joliet, Il 60432',
'T:(815) 774-6343, E:gis@willcountyillinois.com',
'Print date: <dyn type=\"date\" format=\"\"/>'
]

# create variable that joins lines together with new line character
newTxt = "\n".join(txtBlock)

# list the mxds of the workspace folder
for mxdname in arcpy.ListFiles("*.mxd"):
    print "checking document: {}".format(mxdname)
    mxd = arcpy.mapping.MapDocument(ws + "\\" + mxdname)

# find textbox by keywords and give it the new text
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if "PRINT DATE" in elm.text.upper():
            elm.text = newTxt

 

Text box before:

 

and after:

The following trouble shoots were suggested on my previous post: find text element in MXD with wildcard? 

 

1. I printed out the context of the text box before it was replaced and after using the repr() class:

    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if "PRINT DATE" in elm.text.upper():
            print str(elm.text) + "\n"
            print "++++++++++++++below is repr+++++++++++++++++++++"
            elm.text = newTxt
            print repr(elm.text)

 

Print out:

Prepared by the Will County GIS Division
Print Date: 11.2.2018
302 N. Chicago St. Joliet, Il 60432

++++++++++++++below is repr+++++++++++++++++++++
u'Prepared by the Will County GIS Division\n302 N Chicago St. Joliet, Il 60432\nT:(815) 774-6343, E:gis@willcountyillinois.com\nPrint date: <dyn type="date" format=""/>'

 

2. I ran the MXD through the MXD Doctor and Documet Defragmenter. There was no corrupted element and running the script on a defragmented copy of the MXD made no difference.

 

3. There were no changes to the font, font size or centering in the Properties of the text box from old to new. As in, the Properties weren't affected.

 

5. I gave the text box element a name, 'PrintDate'. This didn't seem to have any effect as using the "PRINT DATE" keywords were enough.

    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if "PRINT DATE" in elm.text.upper():
            elm.name == 'PrintDate'

 

6. I know the script works. I tested the script on a new MXD with similar text in a text box. The script ran fine and the resulting new text was displayed correctly! I also had a second text box in the MXD that had the word "date" among the text . I ran the script once with only "DATE" as a key word and it found both text boxes. It knowingly changed both. The odd thing was that while the one I'm after came out illegible again, the second text box was displayed correctly! 

 

I'm wondering if anyone has seen this behaviour before?

Outcomes