Im trying to export data driven pages (Mapbook) and have the resulting PDF named for a/the index field. Here's the code snippit I am working on:
mport arcpy print "Setting Map Document. . ." mxd = arcpy.mapping.MapDocument("CURRENT") for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): mxd.dataDrivenPages.currentPageID = pageNum pageName = mxd.dataDrivenPages.pageRow.stand_key print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str (mxd.dataDrivenPages.pageCount)) arcpy.mapping.ExportToPDF(mxd, r"D:\Temp\Test" + str(pageName) + ".pdf") del mxd
This works just fine, but "Test" is a prefix of the filename. If i remove the "Test" and have and make the code like this below...it doesn't work. Just to be clear I need the individually exported PDF page to be named for the value of str(pageName)
import arcpy ... print "Setting Map Document. . ." ... mxd = arcpy.mapping.MapDocument("CURRENT") ... for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): ... mxd.dataDrivenPages.currentPageID = pageNum ... pageName = mxd.dataDrivenPages.pageRow.stand_key ... print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str (mxd.dataDrivenPages.pageCount)) ... arcpy.mapping.ExportToPDF(mxd, r"D:\Temp" + str(pageName) + ".pdf") ... del mxd
Should I be using something other than arcpy.mapping.ExportToPDF? Other Solution?
Should
Solved! Go to Solution.
This works for me (resulting in '1.pdf', '2.pdf', '3.pdf', etc.):
>>> import os >>> mxd = arcpy.mapping.MapDocument("CURRENT") >>> for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): ... mxd.dataDrivenPages.currentPageID = pageNum ... name = str(pageNum) + ".pdf" ... arcpy.mapping.ExportToPDF(mxd, os.path.join("C:\junk", name))
Does it work if you add a '\' in the path?
arcpy.mapping.ExportToPDF(mxd, r"D:\Temp\" + str(pageName) + ".pdf")
Thanks for the reply.
Nope, sure doesn't...
Okay, what does "it doesn't work" mean? Is there an error? If so, what is it?
This works for me (resulting in '1.pdf', '2.pdf', '3.pdf', etc.):
>>> import os >>> mxd = arcpy.mapping.MapDocument("CURRENT") >>> for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): ... mxd.dataDrivenPages.currentPageID = pageNum ... name = str(pageNum) + ".pdf" ... arcpy.mapping.ExportToPDF(mxd, os.path.join("C:\junk", name))
Sorry for the brevity on my initial response. It threw the error:
Parsing error SyntaxError: EOL while scanning string literal
Your second suggestion worked perfectly though..
Thanks much.
todd