Select to view content in your preferred language

Invalid MXD filename error

5374
11
03-14-2011 04:17 PM
JasonHarshman
Frequent Contributor
I'm trying to modify a python script that was provided by Esri in the sample arcpay mapping script tools. I'd like to export a PDF for each MXD selected instead of exporting multiple MXDs to a single PDF. Below is the Esri version and beneath that is my version. I feel like I'm doing everything right, but obviously I'm missing something. Any guidance is greatly appreciated.

# Author:  ESRI
# Date:    July 5, 2010
# Version: ArcGIS 10.0
# Purpose: This script will export multiple map document layoutinto a single
#          output PDF file. The script is intended to run within a script tool.  There are two
#          parameters:
#               1) Select Map Documents to Append,
#               2) Output PDF.
#
#Notes: The order of the MXDs is based on how they are entered.  The MXD at the
#       top of the list is first followed by those below it.

import arcpy, os, string

#Read input parameters from script tool
mxdList = string.split(arcpy.GetParameterAsText(0), ";")
outPDFpath = arcpy.GetParameterAsText(1)

#Create a new PDF object to store the results
outputPDF = arcpy.mapping.PDFDocumentCreate(outPDFpath)

#Loop through each MXD in the list, export, create a temporary PDF name,
# and append to final, output PDF
for mxdPath in mxdList:
    mxd = arcpy.mapping.MapDocument(mxdPath)
    PDFPath = mxdPath[:-4] + "_temp.pdf"
    arcpy.mapping.ExportToPDF(mxd, PDFPath)
    outputPDF.appendPages(str(PDFPath))


#Save the changes and open the result automatically   
outputPDF.saveAndClose()
os.startfile(outPDFpath)

#Remove variable reference to file
del outputPDF


#MY CODE
import arcpy, os, sys, string

#overwirte existing PDFs
arcpy.OverWriteOutput = 1

multimxds = string.split(arcpy.GetParameterAsText(0), ";")
outputfolder = arcpy.GetParameterAsText(1)

#set variable for PDF name
#trying to set folder path
folderpath = outputfolder

#export to pdf
#trying to export to pdf using title name and folder parameter
for mxdloop in multimxds:
    mxd = arcpy.mapping.MapDocument(mxdloop)
    name = mxd.title
    pdf = folderpath + '\\' + name + ".pdf"
    arcpy.mapping.ExportToPDF(mxd, pdf, 'PAGE_LAYOUT', 640, 480, 400)

#delete folderpath variable
del folderpath
Tags (2)
0 Kudos
11 Replies
JasonScheirer
Esri Alum
Could you post the error messages?
0 Kudos
JasonHarshman
Frequent Contributor
Hi Jason,

This is the error I receive in PythonWin. Attached is a screen shot of the error I receive when running the script as a tool.

Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\Harshman\Desktop\PDF Loop Test\ExportMXDToPDF_LOOP.py", line 16, in <module>
    mxd = arcpy.mapping.MapDocument(mxdloop)
  File "C:\Program Files\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\mixins.py", line 443, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.


Thank you.
0 Kudos
JasonScheirer
Esri Alum
Could you put an AddMessage in your loop to see what file it's failing on?

#MY CODE
import arcpy, os, sys, string

#overwirte existing PDFs
arcpy.OverWriteOutput = 1

multimxds = string.split(arcpy.GetParameterAsText(0), ";")
outputfolder = arcpy.GetParameterAsText(1)

#set variable for PDF name
#trying to set folder path
folderpath = outputfolder

#export to pdf
#trying to export to pdf using title name and folder parameter
for mxdloop in multimxds:
    arcpy.AddMessage("Opening map document %s" % mxdloop)
    mxd = arcpy.mapping.MapDocument(mxdloop)
    name = mxd.title
    pdf = folderpath + '\\' + name + ".pdf"
    arcpy.mapping.ExportToPDF(mxd, pdf, 'PAGE_LAYOUT', 640, 480, 400)
0 Kudos
JasonHarshman
Frequent Contributor
It fails on the first MXD selected in my parameter, but it doesn't seem to matter what MXDs I select or in what order I select the MXDs. It always fails on the first one.
0 Kudos
JasonScheirer
Esri Alum
Could you post the output from the geoprocessing window?
0 Kudos
JasonHarshman
Frequent Contributor
Jason,

Attached is a screen shot of the GP window. Like I said, it fails on the first MXD in the list. Thanks for your help.
0 Kudos
JasonScheirer
Esri Alum
Ah, I see now. Need to strip the quotes.

multimxds = [filename[1:-1] for filename in string.split(arcpy.GetParameterAsText(0), ";")]
0 Kudos
JasonHarshman
Frequent Contributor
I'm not sure I follow. I stripped the quotes and got an invalid syntax error. I used single quotes and got the same invalid MXD error. I inserted the code snippet you provided and i still received the invalid MXD error.

This is what I have:

import arcpy, os, sys, string

#overwirte existing PDFs
arcpy.OverWriteOutput = 1

multimxds = [filename[1:-1] for filename in string.split(arcpy.GetParameterAsText(0), ";")]
outputfolder = arcpy.GetParameterAsText(1)

#set variable for PDF name
#trying to set folder path
folderpath = outputfolder

#export to pdf
#trying to export to pdf using title name and folder parameter
for mxdloop in multimxds:
    mxd = arcpy.mapping.MapDocument(mxdloop)
    name = mxd.title
    pdf = folderpath + '\\' + name + ".pdf"
    arcpy.mapping.ExportToPDF(mxd, pdf, 'PAGE_LAYOUT', 640, 480, 400)

#delete folderpath variable
del folderpath
0 Kudos
JuanOrozco
Regular Contributor
Is failing because your MXD path has spaces in it.
0 Kudos