Select to view content in your preferred language

iterate rows and export to pdf

1620
4
Jump to solution
04-07-2014 10:41 AM
BradSwanson
Emerging Contributor
I am trying to write a code that will iterate through a table, and export each row in the table as a separate PDF. The first time through the loop works. The second time around it gives me the following error message:

Runtime error  Traceback (most recent call last):   File "<string>", line 21, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\analysis.py", line 98, in Select     raise e ExecuteError: ERROR 000732: Input Features: Dataset Congressional_District_7 does not exist or is not supported

I am using ArcGIS desktop 10.1

This is the python code, I have also attached a copy:

import arcpy from arcpy import analysis, env, management  workspace = arcpy.env.workspace = "Y:/Office2/WDIA Mapping/April/Scratch.gdb" #fill in the path for the workspace env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]  distRows = arcpy.SearchCursor('us_house') #sets the search cursor. This may be the problem? #Try an update cursor to resolve looping problem?  for row in distRows:     distName = row.getValue("NAMELSAD")     df.extent = row.SHAPE.extent     df.scale = df.scale * 1.07     unique_name = arcpy.CreateUniqueName(distName)     whereClause = "NAMELSAD <> '%s'" % distName #this results in the shadow     selected_dist = analysis.Select(distName, unique_name, whereClause) #shadow applied to layer     layerList = arcpy.mapping.ListLayers(mxd, "", df)     #apply the symbology to the currentShadow layer     arcpy.ApplySymbologyFromLayer_management (layerList[0], 'usHouseDist_05_shadow')     title = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*")[0]     title.text = "trial run" #this is where we need to format the title so it can be dymanyic     arcpy.mapping.ExportToPDF(mxd, "Y:/Office2/WDIA Mapping/April/firstTrial/district_'%s'" % distName) #remember to fix this line     arcpy.mapping.RemoveLayer(df, layerList[0])     del distName, unique_name     distRows.next()     #arcpy.RefreshActiveView() 
Tags (2)
1 Solution

Accepted Solutions
AmyKlug
Frequent Contributor
If I understand you are trying to iterate through a feature class and create a map for each feature with the feature name as the title? I just use a definition query:

import arcpy  arcpy.env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument(r"U:\Projects\Polygons\Polygons.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] layer = arcpy.mapping.ListLayers(mxd, "Polygons")[0] sc = set(r[0] for r in arcpy.da.SearchCursor(layer, "Field1"))  for s in sc:     where_clause = "Field1 = '"+s+"'"     layer.definitionQuery = where_clause     title = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")[0]     title.title = s     ext = layer.getExtent()     df.extent = ext     arcpy.AddMessage("Making H:/TestExports/FD" + s + ".pdf")     arcpy.mapping.ExportToPDF(mxd, "H:/TestExports/FD" + s + ".pdf")  del mxd, df, layer, sc, s

View solution in original post

0 Kudos
4 Replies
BradSwanson
Emerging Contributor
import arcpy
from arcpy import analysis, env, management

workspace = arcpy.env.workspace = "Y:/Office2/WDIA Mapping/April/Scratch.gdb" #fill in the path for the workspace
env.overwriteOutput = True

mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

distRows = arcpy.SearchCursor('us_house') #sets the search cursor. This may be the problem?
#Try an update cursor to resolve looping problem?

for row in distRows:
distName = row.getValue("NAMELSAD")
df.extent = row.SHAPE.extent
df.scale = df.scale * 1.07
unique_name = arcpy.CreateUniqueName(distName)
whereClause = "NAMELSAD <> '%s'" % distName #this results in the shadow
selected_dist = analysis.Select(distName, unique_name, whereClause) #shadow applied to this layer
layerList = arcpy.mapping.ListLayers(mxd, "", df)
#apply the symbology to the currentShadow layer
arcpy.ApplySymbologyFromLayer_management (layerList[0], 'usHouseDist_05_shadow') #whatever the top layer is
title = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*")[0]
title.text = "trial run" #this is where we need to format the title so it can be dymanyic
arcpy.mapping.ExportToPDF(mxd, "Y:/Office2/WDIA Mapping/April/firstTrial/district_'%s'" % distName) #remember to fix this line
arcpy.mapping.RemoveLayer(df, layerList[0])
del distName, unique_name
distRows.next()
#arcpy.RefreshActiveView() 
0 Kudos
AmyKlug
Frequent Contributor
If I understand you are trying to iterate through a feature class and create a map for each feature with the feature name as the title? I just use a definition query:

import arcpy  arcpy.env.overwriteOutput = True  mxd = arcpy.mapping.MapDocument(r"U:\Projects\Polygons\Polygons.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] layer = arcpy.mapping.ListLayers(mxd, "Polygons")[0] sc = set(r[0] for r in arcpy.da.SearchCursor(layer, "Field1"))  for s in sc:     where_clause = "Field1 = '"+s+"'"     layer.definitionQuery = where_clause     title = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT")[0]     title.title = s     ext = layer.getExtent()     df.extent = ext     arcpy.AddMessage("Making H:/TestExports/FD" + s + ".pdf")     arcpy.mapping.ExportToPDF(mxd, "H:/TestExports/FD" + s + ".pdf")  del mxd, df, layer, sc, s
0 Kudos
MathewCoyle
Honored Contributor
Have you looked at Data Driven Pages?
0 Kudos
BradSwanson
Emerging Contributor
Thank you Amy Klug! The definition query worked! I have looked into data driven pages. I think that is the next step in the project. Thanks again!
0 Kudos