1

Let say I have two variables.

    InputMXD = parameters[0].valueAsText
    PDF_Folder = parameters[1].valueAsText

I have my inputMXDs to have multivalues so I'll have around 10 mxds each time the script is run. I want to put the exported pdf's into different directory, but I want to use whatever the mxd name is. The input could be p:/mxds/oneexample.mxd and p:/mxds/onemoreexample.mxd and the output would be z:/pdfs/oneexample.pdf and p:/mxds/onemoreexample.pdf.

    MXDList = arcpy.ListFiles(InputMXD)
    for MXDPath in MXDList:
        MXD = arcpy.mapping.MapDocument(MXDPath)
        arcpy.mapping.ExportToPDF(MXD, PDF_Folder + )

Here's what I have to get me to the last line. I don't know how to add the original file name of the mxd to the outputted PDF.


Updated: It will run through it, but it doesn't output anything. The output folder is empty. I think there's something wrong with the loop. Here's what I now have in my loop.

    MXDList = arcpy.ListFiles(InputMXD)
    for MXDPath in MXDList:
        MXD = arcpy.mapping.MapDocument(MXDPath)
        outputPDF = os.path.basename(MXDPath).split('.')[0] + ".pdf"
        arcpy.mapping.ExportToPDF(MXD, PDF_Folder + "\\" + outputPDF)

I tried the code below and still no dice. Here's what the results kicked back. enter image description here

I'm not sure why it's saying the output is empty?


Update 2: I updated the code to this:

    arcpy.env.overwriteOutput = True
    arcpy.env.workspace = PDF_Folder
    MXDList = arcpy.ListFiles('*.mxd')
    for MXDPath in MXDList:
        MXD = arcpy.mapping.MapDocument(MXDPath)
        outputPDF = os.path.basename(MXDPath).split('.')[0] + ".pdf"
        arcpy.mapping.ExportToPDF(MXD, PDF_Folder + "\\" + outputPDF)

I changed the workspace to PDF_Folder and it didn't kick back an error. It also didn't output any PDF files.

Also, here's how my parameters are setup at the top.

    # First parameter
    InputMXD = arcpy.Parameter(
        displayName="Input MXDs",
        name="InputMXD",
        datatype="DEMapDocument",
        parameterType="Required",
        direction="Input",
        multiValue=True)

    # Second Parameter
    PDF_Folder = arcpy.Parameter(
        displayName="Output Folder for PDF",
        name="PDF_Folder",
        datatype="DEFolder",
        parameterType="Required",
        direction="Output")

Does my output need to be something besides a DEFolder datatype?


Update 3

Here's what my input looks like:
enter image description here

Input MXDs is a MapDocument that is a multiValue input. Output Folder for PDF is a DEFolder output. I've tried strings, workspaces, folders. Nothing has worked. I know its something small making this really frustrating. I'm going basic to see if I can get just one mxd outputted to a pdf. Crawl before I can walk.


Update 4

I'm trying to run the command directly in ArcMap from the python window. Here is the kickback it's giving me.
Python Window in Arc


Update 5

I have tried just running it in a small script to see if it worked with a file name. It does and it includes spaces, commas, periods, dashes and underscores. Here's the simple testing code that worked. I have no idea what isn't working.

 import arcpy

 doc = arcpy.mapping.MapDocument("Q://Field Data//6th P.M//COLORADO//6N65W//PROJECTS//6N-65W-12//HOLTON 6-65-12//BEP.mxd")
 arcpy.mapping.ExportToPDF(doc, "C://temp//test2.pdf")
  • Editted my answer below - need to set a workspace and then use the arcpy.ListFiles with a wildcard like below – dklassen Feb 4 '14 at 18:54
  • I use typically use the arcpy.GetParameterAsText option to get my workspace and arguments – dklassen Feb 4 '14 at 19:49
  • I thought you had to set the data type in python toolboxes – MjonesGEO Feb 4 '14 at 19:54
  • You can set the data type to String/Text and input it. That will work too! – dklassen Feb 4 '14 at 20:01
  • I've tried different data types, but I think it something else. It's skipping over something. – MjonesGEO Feb 4 '14 at 20:13
2

Give this a try to get the PDF name based on the input MXD lists name:

import os
import arcpy
inws = arcpy.GetParameter(0)
outfolder = arcpy.GetParameter(1)
arcpy.env.workspace = inws
MXDList = arcpy.ListFiles('*.mxd')
for MXDPath in MXDList:
    MXD = arcpy.mapping.MapDocument(MXDPath)
    outputPDF = os.path.basename(MXDPath).split('.')[0] + ".pdf"
    arcpy.mapping.ExportToPDF(MXD, outfolder + "\\" + outputPDF)

The parameters for the toolbox should look something like this: enter image description here

  • Thanks! Would that go inside the loop on a separate line? – MjonesGEO Feb 4 '14 at 18:19
  • You would have to re-initiate the outpdfname for each MXD - so inside the loop - so in your loops script - you would have to use the MXDPath for your input – dklassen Feb 4 '14 at 18:26
  • Since it's a python toolbox, I don't have to deal with adding a script. I've never dealt with that window. All of that data is built in the script. – MjonesGEO Feb 4 '14 at 21:18
  • I have never approached it like this before - I don't know how to write that or impliment it. – dklassen Feb 4 '14 at 21:19
  • No problem. Here are the different data types for python toolboxes. resources.arcgis.com/en/help/main/10.1/index.html#//… – MjonesGEO Feb 4 '14 at 21:22
0

Here's the def execute code from my python toolbox.

    # Get inputs
    InputMXD = parameters[0].valueAsText
    PDF_Folder = parameters[1].valueAsText
    printer = parameters[2].valueAsText

    # Set workspace as defined by user
    env.workspace = InputMXD

    #List Names for Inputs
    arcpy.AddMessage("PDF Folder: "+PDF_Folder)
    arcpy.AddMessage("MXDs before Loop: "+InputMXD)
    arcpy.AddMessage("Printer: "+printer)

    for MXDPath in parameters[0].valueAsText.split(';'):
        #This part outputs to PDF
        arcpy.AddMessage("LOOPING.." + MXDPath)
        outputPDF = os.path.basename(MXDPath).split('.')[0] + ".pdf"
        arcpy.AddMessage("OutputPDF name is: " + outputPDF)
        MXDDoc = arcpy.mapping.MapDocument(MXDPath)
        arcpy.AddMessage("PDF Path: "+ PDF_Folder + "\\" + outputPDF)
        arcpy.mapping.ExportToPDF(MXDDoc, PDF_Folder + "\\" + outputPDF)
        #This part Prints to a certain printer
        arcpy.mapping.PrintMap(MXDDoc, printer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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