Data Driven Pages: Python code to include page index in output file name?

549
4
Jump to solution
11-19-2013 12:24 PM
by Anonymous User
Not applicable
I have used the DDP toolbar to set up DDP where 'Reach' is the index layer (as in river reach).  Each of the 100 Reaches has an ID, stored in the attribute table.  I want the output JPG name to be Reach ID plus _reachmap.jpg, concatenated.  However, instead of Reach ID I am getting a number between 1 and 100.  I have tried replacing pageNum with various other things but I get the same results or an error.  How can I get DDP to 'see' the Reach ID instead of the generic 1 to 100?

import arcpy mxd = arcpy.mapping.MapDocument("CURRENT") for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):     mxd.dataDrivenPages.currentPageID = pageNum     arcpy.mapping.ExportToJPEG(mxd, r"C:\output\\" + str(pageNum) + "_reachmap.jpg") del mxd
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
Using the pageRow property.

So something like arcpy.mapping.ExportToJPEG(mxd, r"C:\output\\" + str(mxd.dataDrivenPages.pageRow.Reach_ID) + "_reachmap.jpg")

View solution in original post

0 Kudos
4 Replies
JasonScheirer
Occasional Contributor III
Using the pageRow property.

So something like arcpy.mapping.ExportToJPEG(mxd, r"C:\output\\" + str(mxd.dataDrivenPages.pageRow.Reach_ID) + "_reachmap.jpg")
0 Kudos
by Anonymous User
Not applicable
Using the pageRow property.

So something like arcpy.mapping.ExportToJPEG(mxd, r"C:\output\\" + str(mxd.dataDrivenPages.pageRow.Reach_ID) + "_reachmap.jpg")


Exactly like that!  I was messing around with getPageIDFromName and getting nowhere.  Thanks much!
0 Kudos
MathewCoyle
Frequent Contributor
Something like this should work if you are referring to your ddp index field value. If it isn't your index field then just insert the name of the field instead of ddp.pageNameField.name

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
ddp = mxd.dataDrivenPages
for pageNum in range(1, ddp.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    id_val = ddp.pageRow.getValue(ddp.pageNameField.name)
    arcpy.mapping.ExportToJPEG(mxd, r"C:\output\{0}_reachmap.jpg".format(id_val))
del mxd
0 Kudos
by Anonymous User
Not applicable
Something like this should work if you are referring to your ddp index field value. If it isn't your index field then just insert the name of the field instead of ddp.pageNameField.name

import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
ddp = mxd.dataDrivenPages
for pageNum in range(1, ddp.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    id_val = ddp.pageRow.getValue(ddp.pageNameField.name)
    arcpy.mapping.ExportToJPEG(mxd, r"C:\output\{0}_reachmap.jpg".format(id_val))
del mxd


Looks good!  I will try both of these options as I build-out my script.  Thanks!
0 Kudos