I am trying to create a text label for a series of maps over time. I have a text box with the element name "DateBox" in my mxd. As a filler, it says "Week # here". I want to it say "Week XX", where XX refers to the week number. I am using arcgis 10.3.
I have tried:
import arcpy
from arcpy import env
env.workspace = r"C:\Users\GIS_User\Output"
map_mxd = arcpy.mapping.MapDocument(r"C:\Users\GIS_User\Output\timelapse.mxd")
for XX in range(1,121):
for TextElement in arcpy.mapping.ListLayoutElements(map_mxd, "TEXT_ELEMENT"):
if TextElementName == "DateBox":
TextElement.text = 'Week ' + str(XX)
print TextElement.text
The arcpy window in ArcMap prints the text and shows that it successfully loops from Week 1 to Week 120. However, this change is not reflected in the textbox of the map document. The textbox itself still holds my original filler text "Week # here" and shows no changes when I run this code.
1 Answer 1
Assuming that you are using the "CURRENT" map I think what you may be missing is:
arcpy.RefreshActiveView()
at the end of each iteration.
-
arcpy.RefreshActiveView()
worked to refresh the view, but did not produce any changes in the displayed text. I am trying to incorporate eitherarcpy.mapping.MapDocument("CURRENT")
ormap_mxd.save()
into the loop. The first runs but produces no changes, while the second gives me this error:Mon Mo– Mon Mo2016年05月25日 00:26:13 +00:00Commented May 25, 2016 at 0:26 -
Traceback (most recent call last): File "<string>", line 8, in <module> File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\utils.py", line 182, in fn_ return fn(*args, **kw) File "c:\program files (x86)\arcgis\desktop10.3\arcpy\arcpy\_mapping.py", line 850, in save return convertArcObjectToPythonObject(self._arc_object.save(*gp_fixargs((), True))) IOError: MapDocObject: Unable to save. Check to make sure you have write access to the specified file and that there is enough space on the storage device to hold your document.
Mon Mo– Mon Mo2016年05月25日 00:29:11 +00:00Commented May 25, 2016 at 0:29 -
@MonMo That sounds like a new question because it would seem to be using a different code snippet to the one presented in this question. Consequently, I think you should research/ask it separately.2016年05月25日 00:43:15 +00:00Commented May 25, 2016 at 0:43
-
Okay, thanks for the feedback. I think it has something to do with transferring the mxd file to a new computer a while back, but you're right, it is a separate topic.Mon Mo– Mon Mo2016年05月25日 01:15:48 +00:00Commented May 25, 2016 at 1:15
-
Turns out,
map_mxd.save()
was not needed. The text on the display was not changing, but checking a jpg export of the map showed thatarcpy.RefreshActiveView()
worked and the text had the appropriate week numbers when the jpg files were viewed separately.Mon Mo– Mon Mo2016年05月25日 19:50:51 +00:00Commented May 25, 2016 at 19:50
map_mxd.save()
to save your MXD after the script is run. Or usearcpy.mapping.MapDocument("CURRENT")
to run it on the currently open MXD.