0

I am working on a program in Arcgis however, I have a python issue. While trying to run the following script I am getting an invalid MXD error which seems to suggest that it cannot find the location of my MXD file.

# 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()

Now please look at the command prompt.

(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.

As you can see, the path I enter is different from what is being returned by print and I think that is what's causing my issue. Would appreciate it anyone could assist fixing handling this path error or with a python script that accomplishes converting MXD to PDF.

Regards

asked Apr 12, 2019 at 18:29
7
  • My advice would be to create a variable in your code that contains the path to the mapDoc file. filename = os.path.join("c:/", "path", "to", "the", "filename.mxd") and pass that to map.MapDocument(filename) Commented Apr 12, 2019 at 18:39
  • @Richard but if i use that I will have to hardcode the .mxd file. I wanted to do it like a batch Commented Apr 12, 2019 at 18:43
  • Sure, we can set up batch processing once we get a simple case working. Commented Apr 12, 2019 at 18:44
  • filename = os.path.join("C:\Users\ant\Documents\MyCensus_Files\Python_script\test.mxd") fullnam = os.path.basename(filename) print filename I have changed it to the above and print shows me the mxd file but i still get the invalid error Commented Apr 12, 2019 at 19:03
  • @Richard It works, but how can i convert everything in the folder vs doing it by sending the file name Commented Apr 12, 2019 at 19:16

2 Answers 2

1

If you print out mapDoc right before the line map.MapDocument(mapDoc), you would see that you're trying to pass C:\Users\ant\Documents\MyCensus_Files\Python script as a mxd file. That's a directory, not a mxd file.

answered Apr 12, 2019 at 19:01
Sign up to request clarification or add additional context in comments.

3 Comments

The idea was to pass the path where the MXD files are so that I can convert them all. But even when I hard code the file name, I still get the error
it works, but how do I convert everything in the path instead of having to pass the file name?
0

Try this:

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)
# ...
answered Apr 12, 2019 at 19:08

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.