Select to view content in your preferred language

Help - Exporting Multiple Map Documents to PDF using Python

4817
13
Jump to solution
08-06-2015 08:06 AM
anayoung
Regular Contributor

Hello,

I am new to coding. I am trying to write a script that will allow me to export multiple map documents to PDF using Python. I found some code to do just that at the following ESRI help link 44242 - Export map documents to PDF using Python . However when I try to run it I get the below Runtime Error message. The only thing I changed from the original code was the workspace pathway. Thank you very much, Ana

>>> import arcpy, os

...

... arcpy.env.workspace = ws = "C:\Users\00177153\Desktop\ArcPy"

...

... mxd_list = arcpy.ListFiles("*.mxd")

...

... for mxd in mxd_files:

...    

...     current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

...     pdf_name = mxd[:-4] + ".pdf"

...     arcpy.mapping.ExportToPDF(current_mxd, pdf_name)

... 

... del mxd_files

...

Runtime Error

Traceback (most recent call last):

     File "<string>", line 7, in <module>

NameError: name 'mxd_files' is not defined

0 Kudos
1 Solution

Accepted Solutions
WesMiller
Deactivated User

I tested this code, try this

import arcpy, os


arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy"


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(ws,mxd[:-4]) + ".pdf"
    print pdf_name
    arcpy.mapping.ExportToPDF(current_mxd, pdf_name)

View solution in original post

13 Replies
WesMiller
Deactivated User

Try changing "mxd_files" to "mxd_list"

anayoung
Regular Contributor

I tried that but reieved the following error message:

Runtime error

Traceback (most recent call last):

     File "<string>", line 7, in <module>

TypeError: 'NoneType' object is not iterable

0 Kudos
IanMurray
Honored Contributor

That would mean that there are no map documents in the filepath you gave.

Also:

arcpy.env.workspace = ws = "C:\Users\00177153\Desktop\ArcPy"

Should just be:

arcpy.env.workspace = "C:\Users\00177153\Desktop\ArcPy"

However, if there are no mxds in that directory, then you will have no items in your list to export, hence the error.

0 Kudos
WesMiller
Deactivated User

Ian Murray​he is using ws on line 9 for readability he should do

arcpy.env.workspace = r"C:\Users\00177153\Desktop\ArcPy"

ws = r"C:\Users\00177153\Desktop\ArcPy"

0 Kudos
IanMurray
Honored Contributor

yea missed that

0 Kudos
WesMiller
Deactivated User

Me too i didn't see it till i reconfigured the code

0 Kudos
WesMiller
Deactivated User

I tested this code, try this

import arcpy, os


arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy"


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(ws,mxd[:-4]) + ".pdf"
    print pdf_name
    arcpy.mapping.ExportToPDF(current_mxd, pdf_name)
anayoung
Regular Contributor

With much appreciation for you - I got it!

See below code that worked (i.e. inserted 'r' and changed mxd_files to mxd_list).

The original code on the ESRI help link should be adjusted. When I tried 'print pdf_name' I got an empty 'test1.pdf'.

>>> import arcpy, os

...

... arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy"

...

... mxd_list = arcpy.ListFiles("*.mxd")

...

... for mxd in mxd_list:

...    

...     current_mxd = arcpy.mapping.MapDocument(os.path.join(ws, mxd))

...     pdf_name = mxd[:-4] + ".pdf"  

...     arcpy.mapping.ExportToPDF(current_mxd, pdf_name)

...

... del mxd_list

My next challenge will be to export the PDFs to another location than the MXDs. I will put in another question for that. Thank you very much.

0 Kudos
WesMiller
Deactivated User

See the adjusted code below

>>> import arcpy, os

...

... arcpy.env.workspace = ws = r"C:\Users\00177153\Desktop\ArcPy"

... pdfws = r"some\other\path

... 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"

...     arcpy.mapping.ExportToPDF(current_mxd, pdf_name)

...

... del mxd_list