for lyr in layerlist: # if the long layer name does not equal the short layer name if lyr.longName != lyr.name: #Find Layer with name == "flow" if lyr.name.lower() == "flow": #Pan to Extent of Flow Layer df.extent = lyr.getExtent() print extent print lyr.name.lower()
lyr = arcpy.mapping.ListLayers(mxd, "Flow", df)[0] df.extent = lyr.getExtent() arcpy.RefreshActiveView()
This should be all you need unless you have some other things going on not in the code you posted.lyr = arcpy.mapping.ListLayers(mxd, "Flow", df)[0] df.extent = lyr.getExtent() arcpy.RefreshActiveView()
Edit: You are just missing the arcpy.RefreshActiveView() I am thinking.
#Import the arcpy package
import arcpy
#Assign the Western States Mapping Document to variable mxd
mxd = arcpy.mapping.MapDocument("CURRENT")
#Create a data driven pages object using "mxd" and assign it to variable "ddp"
#ddp = mxd.dataDrivenPages
#Create a list of all text elements in the map layout
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")
#Cycle through the text elements
for txt in textlist:
#When you find one whose text says "MAP SUBTITLE"...
if txt.text == r"MAP SUBTITLE":
#....Assign it to variable "subtitle"
subtitle = txt
#Create a list of the layers in the map document
layerlist = arcpy.mapping.ListLayers(mxd)
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Cycle through each layer
for lyr in layerlist:
# if the long layer name does not equal the short layer name
if lyr.longName != lyr.name:
#Find Layer with name == "flow"
if lyr.name.lower() == "flow":
#Pan to Extent of Flow Layer
lyr = arcpy.mapping.ListLayers(mxd, "flow",df)[0]
df.extent = lyr.getExtent()
df.scale = df.scale * 1.15
arcpy.RefreshActiveView()
#If the layer is NOT visible...
if lyr.visible == False:
#...Turn the layer on
lyr.visible = True
list2 = arcpy.mapping.ListLayers(lyr)
#Change the Subtitle text element to match the layer's name
subtitle.text = lyr.name
#Create a string showing the path of the pdf that will be written out
pdfpath = "C://Users//user//Desktop//PDF_TEMP//" + lyr.name + r" mapbook.pdf"
#Print that we are exporting a mapbook
print "Exporting " + pdfpath + "....."
#Export the mapbook to pdf
arcpy.mapping.ExportToPDF(mxd, pdfpath)
#Turn the layer off again
lyr.visible = False
#Delete variables to clean up memory usage
del mxd
print "Done!"
lyrlist = arcpy.mapping.ListLayers(mxd, "*flow*")
Are you having a problem with the PDF not exporting at the correct extent, or in your mxd the layer not going to the correct extent?
These flow layers are in a group layer, that's why I assume you are testing for the long name? You can restrict the layer list by just doing something like this.lyrlist = arcpy.mapping.ListLayers(mxd, "*flow*")
#Import the arcpy package
import arcpy
#Assign the Western States Mapping Document to variable mxd
mxd = arcpy.mapping.MapDocument("CURRENT")
#Create a data driven pages object using "mxd" and assign it to variable "ddp"
#ddp = mxd.dataDrivenPages
#Create a list of all text elements in the map layout
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")
#Cycle through the text elements
for txt in textlist:
#When you find one whose text says "MAP SUBTITLE"...
if txt.text == r"MAP SUBTITLE":
#....Assign it to variable "subtitle"
subtitle = txt
#Create a list of the layers in the map document
layerlist = arcpy.mapping.ListLayers(mxd)
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Cycle through each layer
for lyr in layerlist:
# if the long layer name does not equal the short layer name
if lyr.longName != lyr.name:
#Find Layer with name == "flow"
if lyr.name.lower() == "flow":
lyrslist2 = arcpy.mapping.ListLayers(mxd, "*flow*")
#Pan to Extent of Flow Layer
for lyr in lyrslist2:
df.extent = lyrs.getExtent()
#df.scale = df.scale * 1.15
arcpy.RefreshActiveView()
#If the layer is NOT visible...
if lyr.visible == False:
#...Turn the layer on
lyr.visible = True
list2 = arcpy.mapping.ListLayers(lyr)
#Change the Subtitle text element to match the layer's name
subtitle.text = lyr.name
#Create a string showing the path of the pdf that will be written out
pdfpath = "C://Users//atimpson//Desktop//DDP_TEMP//" + lyr.name + r" mapbook.pdf"
#Print that we are exporting a mapbook
print "Exporting " + pdfpath + "....."
#Export the mapbook to pdf
arcpy.mapping.ExportToPDF(mxd, pdfpath)
#Turn the layer off again
lyr.visible = False
#Delete variables to clean up memory usage
del mxd
print "Done!"
df.extent = lyrs.getExtent()
df.extent = lyr.getExtent()
if "flow" in lyr.name.lower():
#Import the arcpy package
import arcpy
#Assign the Western States Mapping Document to variable mxd
mxd = arcpy.mapping.MapDocument("CURRENT")
#Create a data driven pages object using "mxd" and assign it to variable "ddp"
#ddp = mxd.dataDrivenPages
#Create a list of all text elements in the map layout
textlist = arcpy.mapping.ListLayoutElements(mxd,"TEXT_ELEMENT")
#Cycle through the text elements
for txt in textlist:
#When you find one whose text says "MAP SUBTITLE"...
if txt.text == r"MAP SUBTITLE":
#....Assign it to variable "subtitle"
subtitle = txt
#Create a list of the layers in the map document
layerlist = arcpy.mapping.ListLayers(mxd, "*flow*")
df = arcpy.mapping.ListDataFrames(mxd)[0]
#Cycle through each layer
for lyr in layerlist:
# if the long layer name does not equal the short layer name
if lyr.longName != lyr.name:
#Find Layer with name == "flow"
if "flow" in lyr.name.lower():
#Pan to Extent of Flow Layer
df.extent = lyr.getExtent()
#df.scale = df.scale * 1.15
arcpy.RefreshActiveView()
#If the layer is NOT visible...
if lyr.visible == False:
#...Turn the layer on
lyr.visible = True
#Change the Subtitle text element to match the layer's name
subtitle.text = lyr.name
#Create a string showing the path of the pdf that will be written out
pdfpath = "C:/test/pdf/" + lyr.name + r" mapbook.pdf"
#Print that we are exporting a mapbook
print "Exporting " + pdfpath + "....."
#Export the mapbook to pdf
arcpy.mapping.ExportToPDF(mxd, pdfpath)
#Turn the layer off again
lyr.visible = False
#Delete variables to clean up memory usage
del mxd
print "Done!"