0

This code is meant take a shapefile in the current mxd, loop through all of the values in a collumn of said shapefile. For each value it will create a definition query, zoom to features then save a copy of the mxd. When I run the code it completes, but only generates one mxd when I'm expecting about 25.

Is the loop breaking due to duplicate values in the Field_1 collumn? If so how can I work around, if not what is breaking the loop after one single iteration?

import arcpy, os
from arcpy import env
#Overwrite features
arcpy.env.overwriteOutput = True
shape = r"C:\NewProjects\shapes\Meters.shp"
output = r"C:\NewProjects\shapes"
layer = r"C:\NewProjects\shapes\newlyr"
mxd = arcpy.mapping.MapDocument(r"C:\NewProjects\shapes\Try.mxd")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
inset = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
lyr1 = arcpy.mapping.ListLayers(mxd, "Meters", df)[0]  # pick the layer you want to "extract" data from
#For each work order:
rows = arcpy.SearchCursor(lyr1, "", "", "FIELD_1", "FIELD_1 A")
for row in rows:
    name = str(row.getValue('FIELD_1'))
    sqlExp = """"FIELD_1" ='"""  + str(row.getValue('FIELD_1'))+"""'"""    # Create a clause to select only the current record
    lyr1.definitionQuery = sqlExp
    df.extent = lyr1.getSelectedExtent(True)
newMxd = "Figure1_" + name
mxd.saveACopy(r"C:\NewProjects\shapes\\" + newMxd + ".mxd")
print "Saving ", newMxd + ".mxd"
print "All done."
del mxd
1

Try including your code to name and save the mxd within your for loop

  • Somehow that completely slipped me. This worked perfectly. A little more tinkering and I've just saved myself a couple days of work over the next few weeks. Thank you! – mittence Oct 3 '13 at 16:23

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.