Alex,Could use a little more clarification here.So, first of all, are you saying that you want one pdf exported for each FC in your layer or one for each species_ID?IOW, for species_ID = 1, it returns 150 records. Do you want one pdf created that shows all 150 features in the extent, or do you actually want 150 pdf documents, one for each of the species_ID=1 features?(based on last repsonse, I assume you really don't need separate mxd's, just as long as you have separate PDF's ?)Also, need to clarify if you are searching/def query on the voila layer or the poles layer as this code applies it to the cult.shp layer???need to iterate through the layer you are actually applying query to. As is, will set the definitionQuery on a layer "poles" in DF "Layers" to the value of "voila" from the cult.shp layer attribute table. I normally SearchCursor the same FC I'm applying the definition query to, that way there is always a "match".import arcpy, os
shape = r"K:\Working\Alex_Gole\cult.shp"
output = r"K:\Working\Alex_Gole\test\"
output = "in_memory\" ## to save temp pdf "in memory" to save filespace and speed. Not sure if this will work for the pdf tool. If not, delete unless you need individual PDF's after appending.
finalPdf = arcpy.mapping.PDFDocumentCreate(os.path.join(output, "All.pdf")) #ADJUST FILE PATH METHOD
mxd = arcpy.mapping.MapDocument(r"K:\Working\Alex_Gole\Try_new.mxd")
#MyList = [3, 4, 5, 6, 7]
#for values in MyList:
# whereclause = '"voila"' "= " + str(values) ### this is actually setting your whereclause = "voila" = 7 as it is the last in your list, and there is nothing else to "do" here...
dfs = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] ## Have to adjust if you need the query, etc. on more than one dataframe
titleElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT","Figure name")[0] ## Assuming you have created a text element in mxd with Element Name = "Figure name"
labelElem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT","Label")[0] ## Assuming you have created a text element in mxd with Element Name = "Label"
lyr1 = arcpy.mapping.ListLayers(mxd, "poles", dfs)[0] ## Have to adjust if you need the query, etc. on more than one dataframe
sCur = arcpy.SearchCursor(shape)
for row in sCur:
voila_var = str(row.getValue(voila))
letter_var = str(row.getValue(Letter))
fid_var = row.getValue(FID)
whereclause = '"voila" = \'' + voila_var + "' ## creates PDF for each macthing FC
#whereclause = '"FID" = ' + str(fid_var) ## comment out other whereclause and use this one to create PDF for EACH FC, regardless of voila value.
titleElem.text = voila_var
labelElem.text = letter_var
lyr1.definitionQuery = whereclause ## This actually puts defquery on the poles layer using the cult.shp values??
mxd.save() ## need to save chages to mxd before you export to PDF or changes won't be represeted.
#Export each theme to a temporary PDF and append to the final PDFprint "export to pdf"
tmpPdf = os.path.join(output, voila_var + ".pdf") #ADJUST FILE PATH METHOD
arcpy.mapping.ExportToPDF(mxd, tmpPdf, resolution=240)
finalPdf.appendPages(tmpPdf)
print "All done."
del mxd
If this workflow doesn't get you on the right track, let us know the other specifics.Have not tested this specifically, but is right our of one of my working scripts, so the basic structure should work. Might have to adjust variable names, syntax, etc.,R_