Hello,
I have several hundred "City Map" data driven MXDs that I would like to export to PDFs using Python. Right now we have to go into each MXD and export the PDFs; this takes time away from other stuff we could be doing. Is there some readily available code out there somewhere that would allow this function? I have been unable to find such a thing on the internet so far. I am new to coding so I am unable to do this by myself at this point. Your help would be much appreciated. Thanks, Ana
Solved! Go to Solution.
Ana,
You need to add some more code
From the ESRI help, example 1:
ArcGIS Help (10.2, 10.2.1, and 10.2.2)
you need to add a page loop and manage the pdfs
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
pdf_name = os.path.join(pdfws,mxd[:-4])+ ".pdf"
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdf_name) # create the PDF document object
for pageNum in range(1, current_mxd.dataDrivenPages.pageCount + 1😞
current_mxd.dataDrivenPages.currentPageID = pageNum
page_pdf = os.path.join(pdfws,mxd[:-4])+ + str(pageNum) + ".pdf"
arcpy.mapping.ExportToPDF(current_mxd, page_pdf)
pdfDoc.appendPages(page_pdf) # add pages to it
os.remove(page_pdf) # delete the file
pdfDoc.saveAndClose() # save the pdf for the mxd
Doing PNG's is different. There is not a built in function to make one PNG from a list of PNG's.
Have you tried calling the ExportToPdf function of the DataDrivenPages class?
DataDrivenPages
http://desktop.arcgis.com/en/desktop/latest/analyze/arcpy-mapping/datadrivenpages-class.htm
You could use python's os.walk method to locate your map documents and call the ExportToPdf method to create a pdf for each of the found map documents.
Python OS.Walk Method
I have code does that but when I choose an MXD that has data driven pages I only get the first PDF in the series.
I found this line of code (i.e. ddp = mxd.dataDrivenPages) that could be the key but I am unsure of how to fit it into the following script:
arcpy, os, sys
... arcpy.env.workspace = ws = sys.argv[1]
... pdfws = sys.argv[2]
... mxd_list = arcpy.ListFiles("*.mxd")
... # Creating MXD List
... for mxd in mxd_list:
... current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
... pdf_name = os.path.join(pdfws,mxd[:-4])+ ".pdf"
... # Exporting PDF
... print ' Exporting from ', str(current_mxd)
... arcpy.mapping.ExportToPDF(current_mxd,pdf_name)
...
...
... print 'Export Process Completed'
... del mxd_list
Are all the mxds going to be data driven pages? Here is the data driven pages examples
http://resources.arcgis.com/en/help/main/10.2/index.html#//00s300000030000000
Hi Wes,
Yes, all of these MXDs have data driven pages. One data driven MXD is a 56 page series of maps of New Orleans. I've looked at the examples at the link you sent me many times but can find no code that I can identify which exports multiple data driven MXDs to PDFs. As I mentioned earlier, I can apply the above code (which you helped me with) to the data driven MXDs but only get the first page of the series exported. In the case of New Orleans, I need all 56 pages of the map series - not just the first page. How can the above code be adjusted to export from data driven MXDs?
There are some problems with hyperlinks on Geonet that still are not fixed yet.
Just google search DataDrivenPages (arcpy.mapping) and it should pull it up in ESRI help.
Here is an example for you
import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\ParcelAtlas.mxd")
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.ExportToPDF(mxd, r"C:\Project\OutPut\ParcelAtlas_Page" + str(pageNum) + ".png")
del mxd
Hi Ian,
The above code exports each page of a Data Driven Pages series into an individual PNG file. I need to export multiple data driven page series at one time to a particular folder. I have over 300 data driven MXDs; each with many pages in the map series. So, rather than opening each MXD and exporting, I would like to have a script that opens the data driven MXDs and exports the series to a folder. I found no code on that page that does this. Does this make sense?
Ana,
You need to add some more code
From the ESRI help, example 1:
ArcGIS Help (10.2, 10.2.1, and 10.2.2)
you need to add a page loop and manage the pdfs
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
pdf_name = os.path.join(pdfws,mxd[:-4])+ ".pdf"
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdf_name) # create the PDF document object
for pageNum in range(1, current_mxd.dataDrivenPages.pageCount + 1😞
current_mxd.dataDrivenPages.currentPageID = pageNum
page_pdf = os.path.join(pdfws,mxd[:-4])+ + str(pageNum) + ".pdf"
arcpy.mapping.ExportToPDF(current_mxd, page_pdf)
pdfDoc.appendPages(page_pdf) # add pages to it
os.remove(page_pdf) # delete the file
pdfDoc.saveAndClose() # save the pdf for the mxd
Doing PNG's is different. There is not a built in function to make one PNG from a list of PNG's.
Excellent! Thank you, David. That works beautifully. However, I did have to delete the extra "+" before str(pageNum) to get it to run properly.Below is the full code I was working with:
import arcpy, os
arcpy.env.workspace = ws = r"C:\citymaps"
pdfws = r"C:\citymappdfs"
mxd_list = arcpy.ListFiles("*.mxd")
for mxd in mxd_list:
current_mxd = arcpy.mapping.MapDocument(os.path.join(ws,mxd))
pdf_name = os.path.join(pdfws,mxd[:-4])+ ".pdf"
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdf_name) # create the PDF document object
for pageNum in range(1, current_mxd.dataDrivenPages.pageCount + 1):
current_mxd.dataDrivenPages.currentPageID = pageNum
page_pdf = os.path.join(pdfws,mxd[:-4]) + str(pageNum) + ".pdf"
arcpy.mapping.ExportToPDF(current_mxd, page_pdf)
pdfDoc.appendPages(page_pdf) # add pages to it
os.remove(page_pdf) # delete the file
pdfDoc.saveAndClose() # save the pdf for the mxd