1

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, eg: 01(This is the folder),01.mxd,01.pdf, 02(This is the folder), 02.mxd,02.pdf, and so on.

How can I put all the files inside the folders made at the same time?

This is my script:

import arcpy, os
import arcpy
from arcpy import env

wsp = arcpy.env.workspace( 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

#Export to pdf file from each mxd
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    pageName = mxd.dataDrivenPages.pageRow.getValue(pageNameField)
    print u"Exportando página {0} de {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPDF(mxd, r"D:\\SedesolVQB\\Proyectos\\proyecto_PythonEjercicio\\Mapas finales\\" + str(pageName) + ".pdf") #This is the way out where I want all the folder and files

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

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

#Save mxd indivuals
for pageNum in range(1,mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    paginaNombre = mxd.dataDrivenPages.pageRow.getValue(pageNameField)
    mxdNombre = os.path.join(mxdSalida,""+str(paginaNombre)+".mxd")
    mxd.saveACopy(mxdNombre)
    print u"Guardando una copia de:{0}".format(str(paginaNombre))

print "PROCESO TERMINADO"

This the result:

D:\Mapas finales
01<--- This is the folder
01.mxd 
01.pdf
02<--- This is the folder
02.mxd
02.pdf
03<--- This is the folder
03.mxd
03.pdf

How could I put all the files inside the folders at the same time?

1

You need to change this section of code. You need to set the output folder path in this for loop. Currently, your output folder is set to mxdSalida. Try this:

for pageNum in range(1,mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    paginaNombre = mxd.dataDrivenPages.pageRow.getValue(pageNameField)
    mxdSalidaCarpeta = os.path.join(mxdSalida, str(paginaNombre))
    mxdNombre = os.path.join(mxdSalidaCarpeta,""+str(paginaNombre)+".mxd")
    mxd.saveACopy(mxdNombre)
    print u"Guardando una copia de:{0}".format(str(paginaNombre))

EDIT: Entire edited script to remove errors and export mxds and pdfs to correct output folders. I also moved the mxd saving into the second for loop and removed the third for loop as it was not needed. A few errors that were fixed were:

when you use r'path\path', path\path is returned. You had r'path\path', which returns path\\path.

I added a variable for the output folders created from mxdSalida + PageName.

I also moved mxdSalida and mxdOrigen out of the for loop as these variables are not changed by the for loops, so no need to recreate them each iteration of the loop.

import arcpy, os
import arcpy
from arcpy import env

wsp = arcpy.env.workspace( 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)
    print u"Exportando página {0} de {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPDF(mxd, os.path.join(mxdSalidaCarpeta, str(pageName) + ".pdf") 
    mxdNombre = os.path.join(os.path.join(mxdSalida, str(pageName)),str(pageName)+".mxd")
    mxd.saveACopy(mxdNombre)
    print u"Guardando una copia de:{0}".format(str(paginaNombre))

print "PROCESO TERMINADO"
  • jbalk, thanks alot for your idea but I got the same result. I m gonna try another way to fix the script. I think I almost closer it. – Victor Quiroz Barrientos Mar 7 '17 at 15:54
  • I think I figured out what the problem was. I've edited your entire script. Please try it and let me know. Thanks. – jbalk Mar 7 '17 at 23:25
  • jbalk Tremendous help bro!!! Thanks a lot!!! I got what I want but still lack something to do on the script. The result was ok but how put in the same folder the pdf files too. The process to export to pdf format was good. I really appreciate your help. – Victor Quiroz Barrientos Mar 8 '17 at 0:32
  • 1
    If I have helped can you please upvote or accept my answer. I've solved your original question. – jbalk Mar 8 '17 at 1:09
  • Please edit your original question to include the part about putting the pdfs in the same folders. I will edit your script again to include that. – jbalk Mar 8 '17 at 1:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.