Help with Script Tool for Creating PDFs from MXDs

954
10
Jump to solution
02-22-2013 11:19 AM
MichelleCouden1
Occasional Contributor III
I need a little help. I have made a script tool to use to export my mxds into a pdf. It would be a single mxd into a single pdf. I am getting an error that the mxd path is incorrect. I have included my script below. Any help would be great!

# 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
Tags (2)
1 Solution

Accepted Solutions
MichelleCouden1
Occasional Contributor III
Thank You so much Jeff! It works great!

View solution in original post

0 Kudos
10 Replies
ChristiNelson1
Occasional Contributor
not much help, but doesn't a list need to be surrounded by brackets?
0 Kudos
MathewCoyle
Frequent Contributor
What specifically is the error you are getting? Have you tried printing out the values of mxdPath and mxdList to see if they are indeed what you want? You can give this a try.

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

Also remember to use
 tags when posting code to retain indentation.
0 Kudos
MichelleCouden1
Occasional Contributor III
Does your script create a script tool to input several mxds to export as several PDFs. One PDF for each mxd. Or actually, I guess it would be easier to do one mxd at a time right!!
0 Kudos
MathewCoyle
Frequent Contributor
It should be able to handle a list of mxd paths to process on. As long as they all have DDP enabled.
0 Kudos
MichelleCouden1
Occasional Contributor III
Is it alright to add data driven pages when all I have is one page\layout\dataframe per mxd? Meaning, I only have one page not multiple pages per mxd.
0 Kudos
MathewCoyle
Frequent Contributor
I'm afraid I don't follow. What are you using data driven pages for if not for multiple page exports? Could you not just use the default ExportToPDF function in that case?
0 Kudos
MichelleCouden1
Occasional Contributor III
I'll try to explain better. Sorry! I am trying to make a script tool that will make my job a little faster. Here: I have 25 mxds (all have single pages) and I want the script tool to export all those mxds into a PDF for each mxd. For Example: Abilene.mxd into Abilene.pdf and Austin.mxd into Austin.pdf on and on ... I hope this explains where I'm going.
0 Kudos
MathewCoyle
Frequent Contributor
Is there some particular functionality of DDP you need? Are you getting any errors with the new code you were running?
0 Kudos
JeffMoulds
Esri Contributor
Judging from your comment:
all I have is one page\layout\dataframe per mxd
...you may not even need data driven pages. Here is a simple export from a list of mxd names:

import arcpy, os
mxdList = ['C:/temp/Counties.mxd','C:/temp/Counties2.mxd']
for mxdPath in mxdList:
    print mxdPath
    mxd = arcpy.mapping.MapDocument(mxdPath)
    output = mxdPath.replace('.mxd', '.pdf', -1)
    print output
    arcpy.mapping.ExportToPDF(mxd, output)
print 'done'


Here is another sample, that exports all MXDs in a folder to PDF:

import arcpy, os
folderPath = r"C:\Temp\Forum"
for filename in os.listdir(folderPath):
    fullpath = os.path.join(folderPath, filename)
    if os.path.isfile(fullpath):
        basename, extension = os.path.splitext(fullpath)
        if extension.lower() == ".mxd":
            mxd = arcpy.mapping.MapDocument(fullpath)
            arcpy.mapping.ExportToPDF(mxd, basename + '.pdf')


And if you do indeed need to export Data Driven Pages, than this line from your script is wrong:

ddp = mxd


It needs to be:

ddp = mxd.dataDrivenPages


For more info on DDP export, see here.

In your case, putting in print statements for the input mxd and output pdf file path for debugging purposes would be a good idea to make sure you got that right.
0 Kudos