Export Data Driven Pages to PDF without a file Prefix

4169
5
Jump to solution
02-04-2015 10:57 AM
ToddBishop
New Contributor

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

0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

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))

View solution in original post

0 Kudos
5 Replies
DarrenWiens2
MVP Honored Contributor

Does it work if you add a '\' in the path?

arcpy.mapping.ExportToPDF(mxd, r"D:\Temp\" + str(pageName) + ".pdf")

0 Kudos
ToddBishop
New Contributor

Thanks for the reply.

Nope, sure doesn't...

0 Kudos
DarrenWiens2
MVP Honored Contributor

Okay, what does "it doesn't work" mean? Is there an error? If so, what is it?

0 Kudos
DarrenWiens2
MVP Honored Contributor

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))
0 Kudos
ToddBishop
New Contributor

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

0 Kudos