0

The following script has the following objectives: User selected MXD files. Here I use a multivalue parameter for FILE with a filter for MXD in the tool box settings. Changing selected text based on Text Element Name (dynamic text). Saving the mxd as copy : removes the last six characters (string reference to the date), and replaces these with the current date string (or a user specified input).

As it stands it saves a copy by simply adding a suffix of the date and does not remove the original ".mxd" so I get a file name that looks like "C:\Home\GIS_Projects\HabitatEnvironments_150122.mxd_150219.mxd" (note the 2 ".mxd"s) If the original mxd was titled "C:\Home\GIS_Projects\HabitatEnvironments_140629.mxd" the new one (same folder-location) should be the same but with the current date "C:\Home\GIS_Projects\HabitatEnvironments_150219.mxd".

import arcpy
import os
import time
import datetime
from time import strftime


mxdString = arcpy.GetParameterAsText(0) #toolbox parameter workspace
#prepare for multivalue toolbox parameter
mxdList = mxdString.split(";")
arcpy.AddMessage(mxdList)
#set new text variables
#These are set parameters in text properties "Element name"
NewMapTitle = arcpy.GetParameterAsText(1)
NewProjectTitle = arcpy.GetParameterAsText(2)
CurrentDate = strftime("%y%m%d")
#THis is where it should loop through the ListInMXD and replace the text.
for mxdItem in mxdList:
    mxd = arcpy.mapping.MapDocument(mxdItem.strip("'"))
    for titleNew in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "MapTitle"):
        titleNew.text = NewMapTitle
    for projectNew in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "ProjectName"):
        projectNew.text = NewProjectTitle
    #Save a copy of each mxd with current date at the end
    #This is where it bugs.  I need to strip the last six characters and replace them with "CurrentDate".
    mxd.saveACopy(mxdItem + CurrentDate +".mxd")

del mxd
2

Just modify the saveACopy at the end like so:

mxd.saveACopy(mxdItem[:-10] + CurrentDate +".mxd")

The mxdItem is just a string, so you can slice it or shorten it like normal strings. Here's some reference for dealing with strings. The bit I added just clips the last 10 characters from mxdItem (10 characters, not 6, because you need to remove the original ".mxd").

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.