Export Data Driven Pages to MXD with file name by Page Name

10129
11
Jump to solution
01-10-2012 02:23 PM
BrunoDeus
New Contributor III
I`m trying export multiple files created by DDP to ".mxd" once each page has the file name defined by the "page name" or by a "field name". Some what the ArcGis already does to export to ".pdf" whereas it exports single-page documents using the page name for the output file name.

I saw a script from Ideas webpage: Export Data Driven Pages to MXD which is almost perfect to me, but the file name came from Page number.

I`ve tried modify It considering all the information that I saw on another pages (Help, ArcGIS Ideas, Foruns), but it doesn`t works until now.
I would like that somebody can helps me. I've made a copy from the last version of script:

>>>import arcpy, os
... mxdPath = r"C:\...\Projeto_DataDrive.mxd" # The original path is too large
... mxd = arcpy.mapping.MapDocument(mxdPath)
... mxdDir = os.path.dirname(mxdPath)
... for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
... pageName = mxd.dataDrivenPages.pageRow.COD_APRT #This line wasn't here in the original script. The FieldName who is indexing the DDP is COD_APRT
... mxd.dataDrivenPages.currentPageID = pageNum
... mxdName = os.path.join(mxdDir, "DDPMap" + str(pageName) + ".mxd")# I just modified str(pageNum) to str(pageName)
... mxd.saveACopy(mxdName)
... del mxd
...

I will appreciate any help.
Tags (2)
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
If you are simply trying to export all pages using the page name as the output pdf name, try:

import arcpy mxd = arcpy.mapping.MapDocument(r"S:\GIS\Appalachia\Map Projects\Midstream\Exhibits\Gathering Maps\Gathering Mapbook.mxd") ddp = mxd.dataDrivenPages ddp.exportToPDF(r"C:\Temp\Page", "ALL", multiple_files = "PDF_MULTIPLE_FILES_PAGE_NAME"


Will produce a bunch of pages using the DDP index name field with a "Page" prefix.

Jeff

View solution in original post

11 Replies
JeffBarrette
Esri Regular Contributor
I believe the issue was that you were setting the new pageName variable before changing the currentPageID.  I swapped the two lines.  I just tried the following and it worked:

import arcpy, os 
mxdPath = r"C:\Temp\DDP.mxd"
mxd = arcpy.mapping.MapDocument(mxdPath)
mxdDir = os.path.dirname(mxdPath)
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageName = mxd.dataDrivenPages.pageRow.NAME
    mxdName = os.path.join(mxdDir, "_" + str(pageName) + ".mxd")
    print mxdName
    mxd.saveACopy(mxdName)
del mxd


Jeff
BrunoDeus
New Contributor III
I sorry to have came late, but...

It Works, Thanks for helping Jeff!!!
0 Kudos
RickySones
New Contributor
So I am too having a similiar problem, probably more has to do with being new to Python and such. Code is below - What I am trying to do is export my mapbook by the page name I set it to using my data driven page layer. The attribute name is SHEET. The file it is based on is MAPGATHERINGGRD.

import arcpy
mxd = arcpy.mapping.MapDocument("S:\GIS\Appalachia\Map Projects\Midstream\Exhibits\Gathering Maps\Gathering Mapbook.mxd")
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
  mxd.dataDrivenPages.currentPageID = pageNum
  pageName = mxd.dataDrivenPages.pageROW.SHEET
  arcpy.mapping.ExportToPDF(mxd, r"C:\Temp\Gathering_Page" + str(pageName) + ".pdf")

Any guidence would help.

Thanks
0 Kudos
JoelCalhoun
New Contributor III
I don't know for certain, but there might be two issues:

You need to change your mxd path to one of the following:

mxd = arcpy.mapping.MapDocument(r"S:\GIS\Appalachia\Map Projects\Midstream\Exhibits\Gathering Maps\Gathering Mapbook.mxd")
or
mxd = arcpy.mapping.MapDocument("S:\\GIS\Appalachia\\Map Projects\\Midstream\\Exhibits\\Gathering Maps\\Gathering Mapbook.mxd")
or
mxd = arcpy.mapping.MapDocument("S:/GIS/Appalachia/Map Projects/Midstream/Exhibits/Gathering Maps/Gathering Mapbook.mxd")


Also:

Try changing:
pageName = mxd.dataDrivenPages.pageROW.SHEET

To:
pageName = mxd.dataDrivenPages.pageRow.SHEET




I hope that helps,


Joel
0 Kudos
GabrielleGrode
New Contributor
Hi there-
This is exactly what I need.

Could you please provide more detailed instructions for using the python script, as I am new to both python and DDP. I am not sure which elements of the script are personalized and which are generic code.

Many thanks,
Gabrielle
0 Kudos
JeffBarrette
Esri Regular Contributor
If you are simply trying to export all pages using the page name as the output pdf name, try:

import arcpy mxd = arcpy.mapping.MapDocument(r"S:\GIS\Appalachia\Map Projects\Midstream\Exhibits\Gathering Maps\Gathering Mapbook.mxd") ddp = mxd.dataDrivenPages ddp.exportToPDF(r"C:\Temp\Page", "ALL", multiple_files = "PDF_MULTIPLE_FILES_PAGE_NAME"


Will produce a bunch of pages using the DDP index name field with a "Page" prefix.

Jeff
GabrielleGrode
New Contributor
Thanks, Jeff. Actually, I want to export the maps to mxd files so I can do some final tweaking on individual map documents. I would like the individual map documents to be called the name of the DDP. I see the sample codes from the above strings, but I don't know which parts of the code I should personalize to my unique project. Any help is much appreciated. Thanks!
Gabrielle
0 Kudos
JeffBarrette
Esri Regular Contributor
The following code will generate 5 MXDs based on each great lake name.

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\GreatLakes.mxd")
ddp = mxd.dataDrivenPages
for pageNum in range(1, ddp.pageCount + 1):
  ddp.currentPageID = pageNum
  print "Saving %s.mxd" %(ddp.pageRow.getValue(ddp.pageNameField.name))
  mxd.saveACopy(r"C:\Temp\\" + ddp.pageRow.getValue(ddp.pageNameField.name) + ".mxd")
del mxd


Jeff
0 Kudos
MichelleNing
New Contributor II
Hello,
    This thread has been great as I am also new to using python scripts.  I have a python script that works fine within the actual mxd workspace but I would like it to run outside of the mxd workspace.  If I run the the python script outside of the mxd workspace it gives me no errors but results in no PDF's.  Can anyone give me a clue to what I am doing wrong? (To clarify, I want to run the python script as a scheduled windows task). 
Many Thanks,
Michelle
----------------------------------->
import arcpy, string

mxd=arcpy.mapping.MapDocument(r"V:\TEMP\MapBooktest\engGIS\Zoning.mxd")
ddp = mxd.dataDrivenPages
indexLayer = ddp.indexLayer
for pageNum in range(1, ddp.pageCount + 1):
    ddp.currentPageID = pageNum
    pageName = ddp.pageRow.FACET_NO
    arcpy.mapping.ExportToPDF(mxd, r"V:\TEMP\MapBooktest\engGIS\PDFtestMapbook\Zoning_" + pageName + ".pdf")
del mxd
0 Kudos