i would like to complete my script to zoom to layer per definition and export pdf but the script give me bugs when it export pdf and not zooming can any help me
import arcpy, os
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []
layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""
print df.name
with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
for row in cursor:
for item in row:
whereList.append(item)
for wl in whereList:
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
df = layerFile.getExtent()
df.extent = outFile
outFile = imgLocation + "\\" + wl + ".pdf"
arcpy.mapping.ExportToPDF(mxd, outFile)
del mxd
Solved! Go to Solution.
Did what I say work? If so what was the issue and what solved it?
Re converting it to Pro, it will be more than a simple changing of a few things, as there's a different mapping class, camera objects etc. I'd recommend putting this thread to rest and posting an entirely new question.
thanks i will do it but befor i want to add a dynamic text in arcmap each layout want the dynamic lebel change i tried to this but not organized my code
import arcpy, os
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []
layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""
print df.name
with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
for row in cursor:
for item in row:
whereList.append(item)
mxd.activeView = "PAGE_LAYOUT"
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
for wl in whereList:
if elm.text == "title":
elm.text = elm.text.replace(elm,wl)
arcpy.RefreshActiveView()
mxd.save()
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
new_extent = layerFile.getExtent()
df.extent = new_extent
df.scale *= 1.5
arcpy.RefreshActiveView()
outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)
outFile = imgLocation + "\\" + wl + ".pdf"
arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd
If you properly format your code I might be able to assist. Code formatting ... the Community Version - Esri Community
import arcpy, os
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []
layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""
print df.name
with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
for row in cursor:
for item in row:
whereList.append(item)
#the code blow for lebel Dynamic in the layout
mxd.activeView = "PAGE_LAYOUT"
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
for wl in whereList:
if elm.text == "title":
elm.text = elm.text.replace(elm,wl)
arcpy.RefreshActiveView()
mxd.save()
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
new_extent = layerFile.getExtent()
df.extent = new_extent
df.scale *= 1.5
arcpy.RefreshActiveView()
outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)
outFile = imgLocation + "\\" + wl + ".pdf"
arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd
Having the for loop for the text element as the initial loop is a bit off imho. also note your element would have to be named 'title' (in the text element properties on your layout there is a name box where you can specify this)
for wl in whereList:
arcpy.RefreshActiveView()
mxd.save()
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
new_extent = layerFile.getExtent()
df.extent = new_extent
df.scale *= 1.5
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.text == "title":
elm.text = wl
arcpy.RefreshActiveView()
outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)
outFile = imgLocation + "\\" + wl + ".pdf"
arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
i made test and change the lebel to "title" as you see in the screen shot but the pdf exportefd without chang
e the dynamic lebel can you retest the code and see it again
my final look code is this
import arcpy, os
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []
layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""
print df.name
with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
for row in cursor:
for item in row:
whereList.append(item)
for wl in whereList:
arcpy.RefreshActiveView()
mxd.save()
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
new_extent = layerFile.getExtent()
df.extent = new_extent
df.scale *= 1.5
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
if elm.text == "title":
elm.text = wl
arcpy.RefreshActiveView()
outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)
outFile = imgLocation + "\\" + wl + ".pdf"
arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd
ah I think I made a silly error, I'm unable to test this.
side note - why are you using mxd.save() and why have you got outFile variable created then overwritten 2 lines later?
import arcpy, os
arcpy.env.overwriteOutput = True
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layerName = arcpy.GetParameterAsText(0)
fieldName = arcpy.GetParameterAsText(1)
imgLocation = arcpy.GetParameterAsText(2)
whereList = []
layerFile = arcpy.mapping.Layer(layerName)
layerFile.definitionQuery = ""
print df.name
with arcpy.da.SearchCursor(layerName, fieldName) as cursor:
for row in cursor:
for item in row:
whereList.append(item)
for wl in whereList:
arcpy.RefreshActiveView()
mxd.save()
layerFile.definitionQuery = fieldName + "= '" + wl + "'"
new_extent = layerFile.getExtent()
df.extent = new_extent
df.scale *= 1.5
for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "title"):
if elm.name == "title":
elm.text = wl
arcpy.RefreshActiveView()
outFile = os.path.join(imgLocation, wl + ".pdf")
arcpy.AddMessage(outFile)
outFile = imgLocation + "\\" + wl + ".pdf"
arcpy.mapping.ExportToPDF(mxd, outFile, "PAGE_LAYOUT")
del mxd
the code worked
firstly i would like to thank a lot for your effort
i made in mxd save and overwrite beacouse the code write from the field have char like )(.\ and the code run out while was running so
saving mxd to know the last extent while the cod was run
what iam thinking now to put the function .strip() int outfile name to clear char like this ()\|?
.strip() would only remove trailing and leading stuff. Probably best to do something like this:
name = ''
for char in wl:
if char.isalpha():
name+=char
outFile = imgLocation + '\\' + name + '.pdf'