Hello,
I have my script for exporting multiple DDP exports to JPEG format:
>>> import arcpy
... mxd = arcpy.mapping.MapDocument("CURRENT")
... 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.ExportToJPEG(mxd, r"X:\DVDs to burn\South Atlantic Margins\Arcview projects\A3 & A4 Images and Export mxd's\2014 DDP A3 Exports\Images\Free Air" + str(pageNum) + ".jpg")
... del mxd
But I need to only export certain pages at a time, not the whole set!
In this case I want pages 1,3,5,7,9 to be exported.
how can I modify my script for this?
thanks
Solved! Go to Solution.
Theo,
try this
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
pageNumbers = [1,2,3,4,9,10,45] #put your page numbers in brackets separated by commas
for pageNum in pageNumbers:
mxd.dataDrivenPages.currentPageID = pageNum
print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
arcpy.mapping.ExportToJPEG(mxd, r"X:\DVDs to burn\South Atlantic Margins\Arcview projects\A3 & A4 Images and Export mxd's\2014 DDP A3 Exports\Images\Free Air" + str(pageNum) + ".jpg")
del mxd
All you do is put the page numbers in the brackets separated by commas, and it should export only those page numbers, feel free to delete the ones currently in there, they are for representation only. It just takes the list of numbers and go to each of those page numbers and exports it out.
If you are only needing odd numbered pages, you could use an if/else statement and use a modulo to check if there is a remainder when the page number is divided by 2.
So after mxd.dataDrivenPages.currentPageID = pageNum
if int(pageNum) % 2 == 1: print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount)) arcpy.mapping.ExportToJPEG(mxd, r"X:\DVDs to burn\South Atlantic Margins\Arcview projects\A3 & A4 Images and Export mxd's\2014 DDP A3 Exports\Images\Free Air" + str(pageNum) + ".jpg") else: continue
if you there are more than 10 pages and you only wanted odds up to 9, you could put an and statement in that first if statement to check if they page number is less than 10 as well.
the page range could be anything. I just gave 1,3,5,7,9 as an example.
Alright, well you could just make a manual list in your python script, then have only loop through those numbers.
pageNumbers = [Your page numbers here]
for page in pageNumbers:
...
Regardless of how you do this, it is going to take some user input, so depending on how you are picking the pages, there are different ways to do this most efficiently. The first post I had is probably the simplest way for odd pages, but if you need 1,4-9, 18-29, 33-57, then it gets a bit more complicated.
Hi Ian,
choosing the page ranges should be easy, I just don't know how or where to tell python to only export the range I ask.
I'm a python novice 100%. I just copied the code from Arc's help page and change the path names and export format (to .jpeg)
Theo,
try this
import arcpy
mxd = arcpy.mapping.MapDocument("CURRENT")
pageNumbers = [1,2,3,4,9,10,45] #put your page numbers in brackets separated by commas
for pageNum in pageNumbers:
mxd.dataDrivenPages.currentPageID = pageNum
print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
arcpy.mapping.ExportToJPEG(mxd, r"X:\DVDs to burn\South Atlantic Margins\Arcview projects\A3 & A4 Images and Export mxd's\2014 DDP A3 Exports\Images\Free Air" + str(pageNum) + ".jpg")
del mxd
All you do is put the page numbers in the brackets separated by commas, and it should export only those page numbers, feel free to delete the ones currently in there, they are for representation only. It just takes the list of numbers and go to each of those page numbers and exports it out.
Ian, thanks that works great!
I see you also have to delete the bit: 'pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):'
and replace it with what you put.
thanks
One more question, the 2nd last line has a part where you can name the output files, in this case it says 'Free Air', but the + str(pageNum) part adds the page number to the end of it. (ie. Free Air 1.jpg, Free Air 2.jpg etc.). Is there a way of using the name attribute from a field instead of pageNum?
thanks
Ooh nevermind I found how to name the output files using the Name rather than the page number.
Here is my final code, I hope it helps someone else out:
>>> import arcpy
... mxd = arcpy.mapping.MapDocument("CURRENT") #here I choose to export from the current mxd I'm running python from
... pageNumbers = [2,4,6,8] #here I have selected the page range I want exported
... for pageNum in pageNumbers:
... mxd.dataDrivenPages.currentPageID = pageNum
... pageName = mxd.dataDrivenPages.pageRow.Name #here is a line I found elsewhere telling python where it can find the page name
... print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
... arcpy.mapping.ExportToTIFF(mxd, r"C:\FolderForExports\FILEname" + str(pageName) + ".tif") #here I've chose TIFF as my format and told python to add the page name to the end of each exported file
... del mxd