Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I have written a python script to export my mxd into a pdf (not using data driven pages). I am getting the error that my mxd path is incorrect! I have included my script below. Any help would be appreciated.

# Necessary modules
import arcpy, os, string, sys

# For less complicated scripts -
# these 2 imports are necessary to utilize a simpler method of debugging outher than traceback 
import win32ui, win32con

# example debug message to screen ......
# arcpy.AddMessage("mxd_file = " + str(mxd_file))
# val = win32ui.MessageBox("mxd_file = " + str(mxd_file), "title",
                          #win32con.MB_OKCANCEL)

#Paramaters...

mxdList = string.split(arcpy.GetParameterAsText(0), ";")
dir = arcpy.GetParameterAsText(1)

#Loop thru & take the base name of each MXD selected and append all map pages to a single pdf 
# and save to chosen directory......

for mxdPath in mxdList:
    mxd = arcpy.mapping.MapDocument(mxdPath)
    name = mxdPath[:-4] + ".pdf"
    file = dir + os.sep + os.path.basename(name)
    ddp = mxd
    ddp.exportToPDF(file, "ALL")

del mxd, file   
share|improve this question
4  
Have you tried including a message to check 'mxdPath' to ensure it's a valid path? –  egdetti Feb 22 '13 at 21:39
1  
Also, can you give an example of how you are calling the script? Are you using fully qualified paths? –  nicksan Feb 22 '13 at 22:49
add comment

1 Answer

I think the comments have addressed your immediate issue, but here are just a few suggestions for writing cleaner, more "Pythonic" code:

  1. When choosing names for variables (or, more correctly termed in Python, "identifiers"), avoid using names of built-in types and functions. file and dir are examples of these. name is not, but it's pretty generic. Try to use descriptive identifiers, not only to avoid naming conflicts, but to help yourself and others better understand your code.
  2. Take advantage of the standard library (and check out Doug Hellman's PyMOTW site for a whirlwind tour). For example, you could use the functions in the os.path module (see also the PyMOTW article on it) to make your path manipulations more robust and clear to other Python programmers.

    • Instead of:

      name = mxdPath[:-4] + ".pdf"
      

      Use os.path.splitext, os.extsep and the built-in join method for string objects (might as well do os.path.basename here as well):

      pdfBaseName = os.path.basename(os.extsep.join([os.path.splitext(mxdPath)[0], "pdf"]))
      
    • Instead of:

      file = dir + os.sep + os.path.basename(name)
      

      Use os.path.join:

      pdfPath = os.path.join(outputFolder, pdfBaseName)
      

      (moved os.path.basename to previous line and changed your variable names as well)

  3. See PEP 8 - Style Guide for Python Code
  4. See Code Like a Pythonista: Idiomatic Python
  5. Use a real Python IDE, like PyScripter or Eclipse with PyDev. Use their interactive debuggers to step through your code, inspect variables, and generally get a better understanding of what is going on at any moment in your program.
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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