update all text elememts (not working)

1370
13
11-09-2016 10:41 AM
JaredPilbeam1
Occasional Contributor

Hi,

I ran this script in PythonWin. I have folder(s) full of mxd documents that will require annual updates such as the year (date), which is what I was attempting here. This script ran but there were no changes made to the doc.

So, the original doc is in one folder and I want to change the date then save it in another folder. I've attached a PDF of the map I'm attempting to update. The year (2016) is in the title and signature. In the title, the year is grouped with the rest. That may be a problem?

Is the script wrong? Or is the title not properly represented in the text string?

Thanks in advance.

0 Kudos
13 Replies
DarrenWiens2
MVP Honored Contributor

I've done this for hundreds of mxds using a script like below:

import arcpy, os
workspace = r'C:\blahblahblah' # starting directory

for root, dirs, files in os.walk(workspace, topdown=True): # starting traversing directories
    for file in files: # loop through file names
        if file.endswith('.mxd'): # if it's an mxd
            mxd = arcpy.mapping.MapDocument(os.path.join(root,file))
            els = arcpy.mapping.ListLayoutElements(mxd, 'TEXT_ELEMENT')
            for el in els:
                if 'old text' in el.text:
                    el.text = el.text.replace('old text','new text')
            mxd.save()
            del mxd‍‍‍‍‍‍‍‍‍‍‍‍‍

I suspect your problem is that your text box text doesn't equal '2016' but it contains '2016'.

JaredPilbeam1
Occasional Contributor

Darren,

Thanks for the reply. I'm a little confused on part of your script:

    mxd = arcpy.mapping.MapDocument(os.path.join(root,file)) <-- What exactly are the parameters you used for the     MapDocument function here? Because looking in the ArcPy Help, the parameters in that function appear as such:

Using your template, here is what I have so far:

0 Kudos
DarrenWiens2
MVP Honored Contributor

mxd = arcpy.mapping.MapDocument(os.path.join(root,file))

Explanation:

root = the root folder (everything except the filename and extension) for the current mxd file, produced by os.walk

file = the filename and extension for the current mxd file, produced by os.walk

os.path.join(root,file) = the full path to the current mxd file (root + file)

arcpy.mapping.MapDocument(os.path.join(root,file)) = an arcpy map document object for the current mxd file

If it makes it easier, you can split those lines apart like this:

mxd_path = os.path.join(root,file)
mxd = arcpy.mapping.MapDocument(mxd_path)
JaredPilbeam1
Occasional Contributor

Thanks.

I completed the script as such and added mxd.saveACopy supposing that would save it to the new 2017 folder, but then received an error:

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

On line 15, you need to specify a full path to an MXD.  The way it looks now, you are trying to save an MXD as a file system folder.

DarrenWiens2
MVP Honored Contributor

Try passing a full path including '.mxd' rather than the folder name.

JaredPilbeam1
Occasional Contributor

I got the same error after including the document and extension.

0 Kudos
DarrenWiens2
MVP Honored Contributor

May not be the problem, but your path syntax is not necessarily correct - use single forward slash, double backslash, or raw string notation (as you did for the first path).

Other than that, does the folder exist that you're trying to save to? And, obviously, do you have write access?

JaredPilbeam1
Occasional Contributor

This time I ran it using the same path syntax in all instances. It ran and copied over the 1st_District_B.mxd, but that was all it did. Were you saying you used this script to work on all the mxds in a folder/drive? That's what i'm ultimately trying to do here.

0 Kudos