python路径问题

时间:2019-04-12 18:29:24

标签: python python-2.7 arcgis

我正在用Arcgis开发程序,但是我遇到了python问题。 尝试运行以下脚本时,我收到无效的MXD错误,似乎表明它找不到我的MXD文件的位置。

# modified by ESRI for use as a toolbox script tool
## imported sys to get the raw inputs and os to use for path separator
import arcpy, sys, os
# Set OverWrite if files already exist
arcpy.OverWriteOutput = 1

print "Enter folder path:"
mapDoc = raw_input()
#mapDoc = sys.argv[1]
print os.path.dirname(mapDoc)
# set a variable to the full path and file name of the MXD
fullnam = os.path.basename(mapDoc)
# Strip off the MXD file extension and store as string variable for use in the 'out_pdf'
nam = fullnam.strip(".mxd")
print nam

# Commented this out, since it doesnt need to be a parameter when you use the MXD name as the PDF name
##print "Enter save as name:"
##mapName = sys.argv[2]

map = arcpy.mapping
mxd = map.MapDocument(mapDoc)

map_document = mxd
#out_pdf = r"K:\projects" + "\\" + mapName + ".pdf"
#out_pdf = r"K:\projects" + os.sep + mapName + ".pdf"
#out_pdf = os.path.dirname(mapDoc) + os.sep + mapName + ".pdf"
out_pdf = os.path.dirname(mapDoc) + os.sep + nam + ".pdf"

# Set all the parameters as variables here:
data_frame = 'PAGE_LAYOUT'
resolution = "300"
image_quality = "NORMAL"
colorspace = "RGB"
compress_vectors = "True"
image_compression = "DEFLATE"
picture_symbol = 'RASTERIZE_BITMAP'
convert_markers = "False"
embed_fonts = "True"
layers_attributes = "NONE"
georef_info = "False"

# Due to a known issue, the df_export_width and df_export_height must be set to integers in the code:
map.ExportToPDF(map_document, out_pdf, data_frame, 640, 480, resolution, image_quality, colorspace, compress_vectors, image_compression, picture_symbol, convert_markers, embed_fonts, layers_attributes, georef_info)

# This gives feedback in the script tool dialog:
arcpy.GetMessages()

现在,请查看命令提示符。

(arcgispro-py3) C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3>"C:\Us
ers\ant\Documents\MyCensus_Files\Python script\Export2PDF.py"
Enter folder path:
C:\Users\ant\Documents\MyCensus_Files\Python script
C:\Users\ant\Documents\MyCensus_Files
Python script
Traceback (most recent call last):
  File "C:\Users\ant\Documents\MyCensus_Files\Python script\Export2PDF.py
", line 22, in <module>
    mxd = map.MapDocument(mapDoc)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.6\ArcPy\arcpy\arcobjects\mixins.
py", line 651, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(
89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.

如您所见,我输入的路径与打印返回的路径不同,我认为这是导致我出现问题的原因。希望任何人都可以协助解决此路径错误,或者使用将MXD转换为PDF的python脚本来协助解决此问题。

致谢

2 个答案:

答案 0 :(得分:1)

如果在行mapDoc之前打印map.MapDocument(mapDoc),您会发现您正在尝试传递 C:\ Users \ ant \ Documents \ MyCensus_Files \ Python脚本作为mxd文件。那是目录,而不是mxd文件。

答案 1 :(得分:0)

尝试一下:

import arcpy, sys, os
arcpy.OverWriteOutput = 1

mapDoc = os.path.join("C:/", "Users", "ant", "Documents", "MyCensus_Files", "Python_script", "test.mxd") 

map = arcpy.mapping
mxd = map.MapDocument(mapDoc)

# ...