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...
Solved! Go to Solution.
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")
- del mxd
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
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
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
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")
- del mxd
Thanks...now is working like I want it!!!
Regards
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.
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
That's the stuff! Thank you