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

Niana
  • 1,057
  • 2
  • 14
  • 42
  • 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)` – Richard Apr 12 '19 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 – Niana Apr 12 '19 at 18:43
  • Sure, we can set up batch processing once we get a simple case working. – Richard Apr 12 '19 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 – Niana Apr 12 '19 at 19:03
  • @Richard It works, but how can i convert everything in the folder vs doing it by sending the file name – Niana Apr 12 '19 at 19:16
  • Great! Next up, we will want to use `glob` to select all the `.mxd` files in a path, and loop through them, calling `map.MapDocument()` on each and calling the `map.ExportToPDF()` on each – Richard Apr 12 '19 at 19:22
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191762/discussion-between-niana-and-richard). – Niana Apr 12 '19 at 19:36

2 Answers2

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.

hostingutilities.com
  • 8,894
  • 3
  • 41
  • 51
  • 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 – Niana Apr 12 '19 at 19:06
  • it works, but how do I convert everything in the path instead of having to pass the file name? – Niana Apr 12 '19 at 19:16
  • Take a look at this question [https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory](https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory) – hostingutilities.com Apr 12 '19 at 19:20
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)

# ...
Richard
  • 2,226
  • 2
  • 19
  • 35