# Import arcpy module import arcpy # Local variables: TCTquadrants_shp = "C:\\TCTData\\Layers\\PDF Maps\\TCTquadrants.shp" CDA_trail_PDF_Dissolve_shp = "C:\\TCTData\\Layers\\PDF Maps\\CDA_trail_PDF_Dissolve.shp" NL_quadrants_Layer = "NL_quadrants_Layer" NL_trail = "NL_trail" NL_quadrants_shp = "C:\\TCTData\\Test\\NL_quadrants.shp" TCT_quadrants_Layer = "TCT_quadrants_Layer" TCT_pageName = "C:\\TCTData\\Test\\TCT_pageName.shp" # Process: Make Feature Layer (Quadrant) arcpy.MakeFeatureLayer_management(TCTquadrants_shp, NL_quadrants_Layer, "\"quadrant\" = 'NL'", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;Quadrant Quadrant VISIBLE NONE;Number Number VISIBLE NONE;index index VISIBLE NONE;index_more index_more VISIBLE NONE;X X VISIBLE NONE;Y Y VISIBLE NONE;Trail_1 Trail_1 VISIBLE NONE;Trail_2 Trail_2 VISIBLE NONE;Trail_3 Trail_3 VISIBLE NONE;Trail_4 Trail_4 VISIBLE NONE;Trail_5 Trail_5 VISIBLE NONE;Trail_6 Trail_6 VISIBLE NONE;Trail_7 Trail_7 VISIBLE NONE;TrailNames TrailNames VISIBLE NONE;Comment Comment VISIBLE NONE;CreatedBy CreatedBy VISIBLE NONE;UpdatedBy UpdatedBy VISIBLE NONE;State State VISIBLE NONE") # Process: Make Feature Layer (Trail) arcpy.MakeFeatureLayer_management(CDA_trail_PDF_Dissolve_shp, NL_trail, "\"ID_PROVINC\" = '01'", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;ID_TRAILTY ID_TRAILTY VISIBLE NONE;ID_TRAILST ID_TRAILST VISIBLE NONE;Name_Trail Name_Trail VISIBLE NONE;ID_SEGMENT ID_SEGMENT VISIBLE NONE;ID_PROVINC ID_PROVINC VISIBLE NONE;Name Name VISIBLE NONE") # Process: Select Layer By Location arcpy.SelectLayerByLocation_management(NL_quadrants_Layer, "INTERSECT", NL_trail, "", "NEW_SELECTION") # Process: Copy Features arcpy.CopyFeatures_management(NL_quadrants_Layer, NL_quadrants_shp, "", "0", "0", "0") # Process: Select and Copy Feature for Page Name arcpy.CopyFeatures_management(NL_quadrants_shp, TCT_pageName, "", "0", "0", "0") # Export Maps mxd = arcpy.mapping.MapDocument(r"C:\\TCTData\\Test\\Trail_PDF_Maps_layout_NL.mxd") # Use cursor to cycle through TCT_pageName feature class cur = arcpy.SearchCursor(TCT_pageName) row = cur.next() # PDF for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum pageName = row.GetValue("index") arcpy.mapping.ExportToPDF(mxd, r"C:\TCTData\Test\No_" + pageName + ".pdf") row = cur.next() # PNG for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum pageName = row.getvalue("index") row = cur.next() arcpy.mapping.ExportToPNG(mxd, r"C:\TCTData\Test\No_" + pageName + ".png") del mxd, cur, row
Solved! Go to Solution.
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum arcpy.AddMessage("Exporting PDF Map " + str(pageNum) + " of " + str(mxd.dataDrivenPages.pageCount)) pageName = mxd.dataDrivenPages.pageRow.index arcpy.mapping.ExportToPDF(mxd, r"C:\TCTData\MAPS\PDF_MAPS\No_" + pageName + ".pdf", resolution=200)
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum arcpy.AddMessage("Exporting PDF Map " + str(pageNum) + " of " + str(mxd.dataDrivenPages.pageCount)) pageName = mxd.dataDrivenPages.pageRow.index arcpy.mapping.ExportToPDF(mxd, r"C:\TCTData\MAPS\PDF_MAPS\No_" + pageName + ".pdf", resolution=200)
Hello Charles-Andre Roy ,
I have a question related to yours (which you successfully solved). Whenever I try to set a string variable to mxd.dataDrivenPages.pageRow.index I get the error: "RuntimeError: Row: Field index does not exist" ... I am trying to export data driven pages to adobe illustrator using the page name. Here's what I have:
import arcpy, os, sys, string from arcpy import env mxd = arcpy.mapping.MapDocument("CURRENT") for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum arcpy.AddMessage("Exporting PDF Map " + str(pageNum) + " of " + str(mxd.dataDrivenPages.pageCount)) pageName = mxd.dataDrivenPages.pageRow.index arcpy.mapping.ExportToAI(mxd, r"C:\Dropbox (WSA)\Tucson\PROJECTS ACTIVE\GSA\2014-58 Richfield Travel Routes\Maps\Site_Location\Site_Location_Map\_" + pageName + ".ai") del mxd
I'd appreciate any help! Thanks!
Jimmy
Hi Jimmy,
I wrote this code a long time ago, but as far as I remember it, the name of the field in the feature class layer is called "index". Remember that python is case sensitive, so "Index" is not the same as "index".
Thank you Charles-Andre Roy. I thought I had tried replacing index with my field name already, but somehow I got my wires crossed. All is working now.
Happy to help!
Hi Karl,
I didn't look back to this since 3 years, so maybe the new arcpy allows to get the pageName automatically from the index field selected from the ArcMap dialog. But 3 years ago, the arcpy module was just getting the pageNum for the filename. The name field could be selected as an user input as well.