How export files pdf into multiple folders with the same name?

615
1
Jump to solution
03-08-2017 05:28 PM
VictorQuiroz1
New Contributor

I am not an expert on Python scripting but I undertand a few concepts about it.

 

I made a script that export multiple individual mxd and pdf with a specific same name, also on my script, I can make multiple folders with the same name from mxd, but I dont know how put all the files on the same folders made.

Is it possible export all the files mxd, pdf which has the same name into folders with the same name?

This my script:

import arcpy, os
import arcpy
from arcpy import env

wsp = r"D:\SedesolVQB\Proyectos\proyecto_PythonEjercicio\PracticasPython.mdb"
Shp = 'Estados'
pageNameField = "CVE_ENT" #<----this is the main field comun to save on folders, mxs and pdf

mxdSalida = r"D:\SedesolVQB\Proyectos\proyecto_PythonEjercicio\Mapas finales"
mxdOrigen = r"D:\SedesolVQB\Proyectos\proyecto_PythonEjercicio\Mxd\Practicas.mxd" #<---This is the maind mxd

#To Make diferent folder from pageNameField
with arcpy.da.SearchCursor(Shp,(pageNameField)) as cursor:
for folder, in cursor:
os.makedirs(os.path.join(mxdSalida, str(folder))) 

#Export to pdf file from each mxd and save mxd indivuals
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)

arcpy.mapping.ExportToPDF(mxd, os.path.join(mxdSalida, str(pageName) + ".pdf"))
mxdNombre = os.path.join(os.path.join(mxdSalida, str(pageName)),str(pageName)+".mxd")
mxd.saveACopy(mxdNombre)

print u"Exportando página {0} de {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
print u"Guardando una copia de:{0}".format(str(pageName))

print "PROCESO TERMINADO"

The result is something like this:

D:\SedesolVQB\Proyectos\proyecto_PythonEjercicio\Mapas finales\01\01.mxd\\---> 01.pdf (Is shown outside)

D:\SedesolVQB\Proyectos\proyecto_PythonEjercicio\Mapas finales\02\02.mxd\\---> 02.pdf (Is shown outside)

D:\SedesolVQB\Proyectos\proyecto_PythonEjercicio\Mapas finales\03\03.mxd\\---> 03.pdf (Is shown outside)

However, the file pdf remains outside from the folders, what I need all the files mxd and pdf into de the folders whit the same name.

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Frequent Contributor

Hi Victor,

First off, for future reference when posting any sort of code on GeoNet, please use the code formatting so its easier for people to read.  https://community.esri.com/blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=9e68e...

From your post it is my understanding that you want your MXD and PDF file for the map to be saved to the same number directory that was created from your search cursor and matches page number attribute of your DDP, but the pdf keeps ending up a directory above the MXD file(Mapas finales instead of the correct number folder).  I believe your issue would be in the fact that you are adding an extra directory to your filepath when creating your mxdNombre variable (mxdNombre = os.path.join(os.path.join(mxdSalida, str(pageName)),str(pageName)+".mxd")) whereas that extra folder path is not included when exporting the PDF (arcpy.mapping.ExportToPDF(mxd, os.path.join(mxdSalida, str(pageName) + ".pdf"))). 

If you added the extra os.path.join which joins the extra folder name to your PDF export, that should fix your problem.

arcpy.mapping.ExportToPDF(mxd, os.path.join(os.path.join(mxdSalida, str(pageName)),str(pageName) + ".pdf"))

Best wishes,

Ian

View solution in original post

0 Kudos
1 Reply
IanMurray
Frequent Contributor

Hi Victor,

First off, for future reference when posting any sort of code on GeoNet, please use the code formatting so its easier for people to read.  https://community.esri.com/blogs/dan_patterson/2016/08/14/script-formatting?sr=search&searchId=9e68e...

From your post it is my understanding that you want your MXD and PDF file for the map to be saved to the same number directory that was created from your search cursor and matches page number attribute of your DDP, but the pdf keeps ending up a directory above the MXD file(Mapas finales instead of the correct number folder).  I believe your issue would be in the fact that you are adding an extra directory to your filepath when creating your mxdNombre variable (mxdNombre = os.path.join(os.path.join(mxdSalida, str(pageName)),str(pageName)+".mxd")) whereas that extra folder path is not included when exporting the PDF (arcpy.mapping.ExportToPDF(mxd, os.path.join(mxdSalida, str(pageName) + ".pdf"))). 

If you added the extra os.path.join which joins the extra folder name to your PDF export, that should fix your problem.

arcpy.mapping.ExportToPDF(mxd, os.path.join(os.path.join(mxdSalida, str(pageName)),str(pageName) + ".pdf"))

Best wishes,

Ian

0 Kudos