3

I've very new to coding anything so sorry if this is an easy fix.

When I export my map, everything is fine and my script replaces the text perfectly. However, when I open my map document manually to visualize it, the text is still the same as before I replaced it. I've even exported one of the maps from that folder without replacing text and it shows that the text has been replaced. I'm not sure what's going on. I used arcpy.RefreshActiveView as well, before mxd.save() and it still doesn't work. Any help would be appreciated.

import arcpy 
import os
import glob
folder = r"C:\Workspace\MapTest"
oldtext = '7351'
newtext = '7352'
mxds = glob.glob(folder + '\\' + '*.mxd')
arcpy.gp.overwriteOutput = True
for mxdFile in mxds:
    mxd = arcpy.mapping.MapDocument(mxdFile)
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.text ==oldtext:
            elm.text = newtext          
    eps = mxdFile.replace('mxd', 'eps')
    arcpy.mapping.ExportToEPS(mxd,eps)
    mxd.save()
del mxd

Also, I'm not really sure how to export my eps documents into another folder. I would be grateful for help on that as well.

Whymarrh
  • 13,139
  • 14
  • 57
  • 108
  • 1
    I made a test mxd with the described text element and tried your script here. It worked perfectly, changed the element in the mxd, etc. Are you by chance doing this with the mxd open in Arcmap? – MikeHunter Nov 20 '12 at 23:11
  • 1
    How are you running this script? In Idle, Pythonwin? The Arcmap Python Window? – MikeHunter Nov 20 '12 at 23:30

1 Answers1

1

Well, I can't help with the mxd saving problem--your script works perfectly for me. To save the eps to another folder, do this:

bn = os.path.basename(mxdFile)[:-3]
eps = os.path.join('C:\\temp', bn + 'eps')
arcpy.mapping.ExportToEPS(mxd, eps)
mxd.save() 

Sorry I can't help with the saving business. ESRI products sometimes do weird things, and it's not unusual for a piece of code to work on one machine and fail on another. Your code looks just fine to me, and does what it's supposed to in my tests.

good luck,

Mike

MikeHunter
  • 4,144
  • 1
  • 19
  • 14
  • Sorry for getting back so late Mike and thank you for the quick response. Maybe I'll restart my computer. I'm runninng it in the Arcmap Python Window from a blank Arcmap template. The code seems to work great, but for some reason it won't visually show me that the document was saved with the new text. – Randy Deodat Nov 21 '12 at 00:24
  • Just an update on this. I had to manually change every text by opening up each arcmap document. We just upgraded to 10.1 and it worked perfectly, just like you said. That was extremely frustrating. – Randy Deodat Dec 19 '12 at 00:06