Select to view content in your preferred language

Invalid MXD filename error

6279
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
JasonHarshman
Frequent Contributor
The above code actually does work, even with spaces in the path name. I think something was hung up in memory because the code works fine now and it's exactly the same.

I am running into the same issue for an expanded version of this script. Basically, the script now exports to PDF a batch of MXDs using either their basename (minus the '.mxd') or the string within the title property. I've restarted my computer just to make sure all memory is cleared yet I still receive the Invalid MXD filename error. The interesting part is that the script will execute when the first IF condition is met, but if won't execute the ELSE condition. When it goes to execute the ELSE condition, it appears that the mxd variable is somehow passing off an invalid filename, but how is that possible if it mxd variable works when the IF condition is met?

Here's the code:
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 folder location
folderPath = outputFolder

#export to pdf
for mxdLoop in multimxds:
    mxd = arcpy.mapping.MapDocument(mxdLoop)
    #use the mxd name as the pdf name
    if mxd.title == "":
        #get the name of the mxd
        mapName = os.path.basename(mxdLoop)[0:-4]
        #create the pdf file path and name
        pdfName = folderPath + '\\' + mapName + ".pdf"
        #export to pdf
        arcpy.mapping.ExportToPDF(mxd, pdfName, 'PAGE_LAYOUT', 640, 480, 300)
    #use the mxd title property as the pdf name
    else:
        #get the title string
        mxdTitle = mxd.title
        #create the pdf file path and name
        pdfName = folderPath + '\\' + mxdTitle + ".pdf"
        #export to pdf
        arcpy.mapping.ExportToPDF(mxd, pdfName, 'PAGE_LAYOUT', 640, 480, 300)
                
#delete variables
del folderPath
0 Kudos
HarmenMolenaar
Emerging Contributor
Dear Jason,

I tried your first code, and am getting the Invalid MXD name error as well. When I added a print command to multimxds, it showed me that the name had chaged to *.mx, e.g. the letter D is missing from the extension.

I think that is coming from this bit:
multimxds = [filename[1:-1]

Although I am not very experienced with arcpy yet.

I hope it helps
0 Kudos