How to export pdf from data driven pages using page name?

6456
8
Jump to solution
02-20-2015 01:10 PM
Ulises
by
Occasional Contributor III

I want to export pdf from data driven pages from a standalone python script.  So far I have the following code that works fine...

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Workspace\DELETEME\TestDDP\dds_test.mxd")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPDF(mxd, r"C:\Workspace\DELETEME\TestDDP" + str(pageNum) + ".pdf")
del mxd

The issue is that I want the file to be name as the page name and not the page ID.  Thing is that my grid feature class jumps from grid_id 123 to 125, but page number shows the sequential number of pages, altought I set page number field to grid_id also.  Grid_ID and GRID_NAME in my feature class have the same value, but due to the jump in values, the generated pdf file 124 is actually representing the map for grid 125.

Any help is appreciated...

Ulises Feliciano Troche
1 Solution

Accepted Solutions
ChristianWells
Esri Regular Contributor

In the example, I sent it looks like I forgot to paste the new page number. Can you try the following?

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Workspace\DELETEME\TestDDP\dds_test.mxd")
pageNameField = "<FieldName>"
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
     mxd.dataDrivenPages.currentPageID = pageNum
     pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)
     print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
     arcpy.mapping.ExportToPDF(mxd, r"C:\Workspace\DELETEME\TestDDP" + str(pageName) + ".pdf"
  1. del mxd 

View solution in original post

8 Replies
ChristianWells
Esri Regular Contributor

Hi Ulises,

Try out the following to get the page name in your loop.

pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Workspace\DELETEME\TestDDP\dds_test.mxd")
pageNameField = "<FieldName>"
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
     pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)
     print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))    
     arcpy.mapping.ExportToPDF(mxd, r"C:\Workspace\DELETEME\TestDDP" + str(pageName) + ".pdf") 
del mxd
0 Kudos
ChristianWells
Esri Regular Contributor

In addition, you can use the following to get out the field name and then feed it back into your row.getValue() statement:

fieldName = mxd.dataDrivenPages.pageNameField.name

0 Kudos
Ulises
by
Occasional Contributor III

Thanks for the reply, Christian.

Tried your suggestion and looks like the way to go, but now I keep getting the same ddp exported in the loop...

"Exporting page 344 of 475"

"Exporting page 344 of 475"

"Exporting page 344 of 475"

"Exporting page 344 of 475"

can't see where to fix it and how to make the pages export in order.

Thanks

Ulises Feliciano Troche
0 Kudos
ChristianWells
Esri Regular Contributor

In the example, I sent it looks like I forgot to paste the new page number. Can you try the following?

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Workspace\DELETEME\TestDDP\dds_test.mxd")
pageNameField = "<FieldName>"
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
     mxd.dataDrivenPages.currentPageID = pageNum
     pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)
     print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
     arcpy.mapping.ExportToPDF(mxd, r"C:\Workspace\DELETEME\TestDDP" + str(pageName) + ".pdf"
  1. del mxd 
Ulises
by
Occasional Contributor III

Thanks...now is working like I want it!!!

Regards

Ulises Feliciano Troche
0 Kudos
williamcarr
Occasional Contributor II

What would be the protocol for using only the page name?

arcpy.mapping.ExportToPDF(mxd, r"C:\Path\folder\" + str(pageName)+ " " + ".pdf")

Throws an EOL error. I am forced to use space or text  after the folder path string, that causes issues with my match table.

0 Kudos
Ulises
by
Occasional Contributor III

William, I solved that by not using raw path (r).  Instead use the inverted dashes.

    arcpy.mapping.ExportToPDF(mxd, "C:/Path/folder/" + str(pageName)+ " " + ".pdf")

that worked fo me

Ulises Feliciano Troche
0 Kudos
williamcarr
Occasional Contributor II

That's the stuff! Thank you

0 Kudos