【发布时间】:2019-09-03 13:44:55
【问题描述】:
我正在 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.
如您所见,我输入的路径与 print 返回的路径不同,我认为这是导致我的问题的原因。任何人都可以帮助解决此路径错误或使用完成将 MXD 转换为 PDF 的 python 脚本,将不胜感激。
问候
【问题讨论】:
-
我的建议是在您的代码中创建一个包含
mapDoc文件路径的变量。filename = os.path.join("c:/", "path", "to", "the", "filename.mxd")并将其传递给map.MapDocument(filename) -
@Richard 但如果我使用它,我将不得不对 .mxd 文件进行硬编码。我想像批处理一样做
-
当然,一旦我们得到一个简单的案例,我们就可以设置批处理。
-
filename = os.path.join("C:\Users\ant\Documents\MyCensus_Files\Python_script\test.mxd") fullnam = os.path.basename(filename) print filename我已将其更改为上述内容并打印显示 mxd 文件,但我仍然收到无效错误 -
@Richard 它可以工作,但我如何转换文件夹中的所有内容,而不是通过发送文件名来实现
标签: python python-2.7 arcgis