Select to view content in your preferred language

MXD export to PDF using Python

1867
4
Jump to solution
11-25-2020 12:57 AM
by Anonymous User
Not applicable

Hello All,

I am new to Python coding, but somehow managed to put together a simple code by looking at others code available online.

What I want to achieve: A user should select a custom designed template MXD which initially will be loaded only with basemap layers. Then the user will add a raster file from local path to the MXD. User will Type in the Name of the Raster Layer to be displayed in TOC. Then the code will export the MXD to a PDF file.

Below mentioned code works well when I run it in the same MXD's Python command window.

mxdPath = r"C:\MXD.mxd" #Paratmeter 1
... rasterPath = r"C:\Raster.tft" #Paratmeter 2
... mxd = arcpy.mapping.MapDocument(mxdPath)
... df = arcpy.mapping.ListDataFrames(mxd)[0]
... result = arcpy.MakeRasterLayer_management(rasterPath,"Raster Layer Name") #Parameter 3
... layer = result.getOutput(0)
... arcpy.mapping.AddLayer(df,layer, 'AUTO_ARRANGE')
... arcpy.mapping.ExportToPDF(mxd,r"C:\Test.pdf") #Parameter 4

I have two queries, and will really appreciate if someone can guide me in the right direction. 

1. How can I let user update the 4 parameters in the code without opening the code

I have seen online add parameters while adding script in the tool box but not sure how to pass them in the code.

Required Parameters 

  • Input Template MXD Path
  • Input Raster File Path
  • Input Type Raster File Name (To be entered by the User to update in TOC)
  • Output PDF path

2. Is it possible to rename the PDF as per the Raster File Name the user types in the parameter 3

Will really appreciate any help and pointers in the right direction.

Thanks and Regards,

Ajinkya 

Tags (1)
0 Kudos
1 Solution

Accepted Solutions
DavidPike
MVP Frequent Contributor

You just need  define the parameters e.g.

mxdPath = arcpy.GetParameterAsText(0)
rasterPath = arcpy.GetParameterAsText(1)

and so on..

Then you just create a script tool for it and define the parameter types in the options of the tool.  When the tool opens, those defined parameters will be there.

 

Add a script tool—ArcGIS Pro | Documentation

What is a script tool?—Help | ArcGIS for Desktop

 

View solution in original post

4 Replies
DavidPike
MVP Frequent Contributor

You just need  define the parameters e.g.

mxdPath = arcpy.GetParameterAsText(0)
rasterPath = arcpy.GetParameterAsText(1)

and so on..

Then you just create a script tool for it and define the parameter types in the options of the tool.  When the tool opens, those defined parameters will be there.

 

Add a script tool—ArcGIS Pro | Documentation

What is a script tool?—Help | ArcGIS for Desktop

 

by Anonymous User
Not applicable

Hi David,

Thanks a lot of your response and suggestion. I will try this.

Much appreciated!!

Ajinkya

0 Kudos
by Anonymous User
Not applicable

Hi David,

It worked !!!😊 , you are best.

One last query is there anyway I can refresh the Layout or TOC in the template MXD to make it ready for next run.

Some how the old raster layers Legend is still getting printed along with new raster layer legend in the output PDF, even if the old raster layer is not there in TOC.

Below is the modified code.

import arcpy

from arcpy import env

#pass the product mxd into the script as an input parameter
mxdPath = arcpy.GetParameterAsText(0)

#Input parameter value Raster path
rasterPath = arcpy.GetParameterAsText(1)

#Input Raster Layer Name Text
Rastername = arcpy.GetParameterAsText(2)

#OutputPDF
pdfname = arcpy.GetParameterAsText(3)


mxd = arcpy.mapping.MapDocument(mxdPath)
df = arcpy.mapping.ListDataFrames(mxd)[0]
result = arcpy.MakeRasterLayer_management(rasterPath, Rastername)
layer = result.getOutput(0)
arcpy.mapping.AddLayer(df,layer, 'AUTO_ARRANGE')
arcpy.mapping.ExportToPDF(mxd,pdfname)

Thanks again for your valuable suggestion.

Ajinkya 

0 Kudos
DavidPike
MVP Frequent Contributor

you might try arcpy.RefreshActiveView()

but i'll have  look at the code properly when I get the chance.

0 Kudos