Arcpy 在ArcToolbox里的使用

今天试验了一下python脚本在ArcToolbox里的使用

1、脚本,用的是esri的脚本,如下:

# 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 sys
reload(sys)
sys.setdefaultencoding('utf-8')

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

2、在ArcCatalog里面新建一个Toolbox,如下图

3、右键单击新建的Toolbox, add->script,弹出添加脚本对话框:

下一步,选择脚本文件:

下一步,设置输入和输出参数:

Finish完整的工具箱中脚本的制作,界面如下:

注意:

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

这三句话一定要加,不然会出现如下的错误:

 UnicodeEncodeError: ‘ascii’ codec can’t encode characters in position 3-6: ordinal not in range(128) 

转载自:https://blog.csdn.net/sprintwater/article/details/30251625

You may also like...