Errno: 10054 when batch exporting mxd to pdf using arcpy.mapping - ArcGIS 10 SP2

1949
13
07-20-2011 02:22 PM
DonovanCameron
Occasional Contributor II
I wrote a small python script that exports all mxd's in a folder to PDF's, but I get the following error in PyScripter and I don't know why...:
[ATTACH]7866[/ATTACH]

The python interpreter has whole bunch of red in it:
Found 5 mxds for PDF exporting...
Directory: C:\Users\is0009\Desktop\temp

Exporting Rural_OCP_map_1_17x11.mxd to:
C:\Users\is0009\Desktop\temp\Rural_OCP_map_1_17x11.pdf
Traceback (most recent call last):
  File "<string>", line 70, in execInThread
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc-python2x.zip\rpyc\core\netref.py", line 123, in __call__
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc-python2x.zip\rpyc\core\netref.py", line 45, in syncreq
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc-python2x.zip\rpyc\core\protocol.py", line 343, in sync_request
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc-python2x.zip\rpyc\core\protocol.py", line 305, in serve
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc-python2x.zip\rpyc\core\protocol.py", line 265, in _recv
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc-python2x.zip\rpyc\core\channel.py", line 36, in recv
  File "C:\Program Files (x86)\PyScripter\Lib\rpyc-python2x.zip\rpyc\core\stream.py", line 105, in read
EOFError: [Errno 10054] An existing connection was forcibly closed by the remote host


Below is my script that I am trying:
The script is placed in a folder with a bunch of mxds.

#Import python modules
import os, sys, string, glob, arcpy
from arcpy import mapping

#Set local variables
mxdLst = glob.glob('*.mxd')
mxdCnt = len(mxdLst)
####
#Place script in same folder as MXDs to get 'current working directory'
####
baseF = os.getcwd()

#Print how many mxd's found
print '\n' + 'Found ' + str(mxdCnt) + ' mxds for PDF exporting...'
print 'Directory: ' + str(baseF) + '\n'

#Loop to process each mxd into a PDF
for mxds in mxdLst:
    PDFr = mxds.replace('mxd', 'pdf') #Replace 'mxd' extension with 'pdf'
    print 'Exporting ' + str(mxds) + ' to:' + '\n' + str(baseF) + '\\' + str(PDFr) #Print current mxd exporting and output pdf name...
    inMxd = arcpy.mapping.MapDocument(mxds) #Make current mxd in loop the mapdocument
    arcpy.mapping.ExportToPDF(inMxd, PDFr) #Export mapdocument to pdf
    print 'Done exporting: ' + str(PDFr)
del mxds
Tags (2)
0 Kudos
13 Replies
DonovanCameron
Occasional Contributor II
Woohoo, new error.


The script below:
#Import python modules
import os, sys, string, glob, arcpy.mapping

#Set local variables
mxdLst = glob.glob('*.mxd')
mxdCnt = len(mxdLst)

####
#Place script in same folder as MXDs to get 'current working directory'
####
baseF = os.getcwd()

#Print how many mxd's found
print '\n' + 'Found ' + str(mxdCnt) + ' MXDs for exporting to PDF...'
print 'Directory: ' + baseF + '\n'


#For loop to process each mxd into a PDF
for mxds in mxdLst:
    #outPDF = mxds.replace('mxd', 'pdf') #Replace 'mxd' extension with 'pdf'
    #print 'Exporting ' + mxds + ' -> ' + outPDF
    mxd = 'r' + '\"' + baseF + '\\' + mxds + '\"' #mxd with fullpathname
    #inMxd = arcpy.mapping.MapDocument(mxd) #Make current mxd in loop the mapdocument
    #arcpy.mapping.ExportToPDF(inMxd, outPDF) #Export mapdocument to pdf
    #print 'Done exporting: ' + outPDF
    print mxd
del mxds


Outputs:
Found 5 mxds for PDF exporting...
Directory: C:\Users\is0009\Desktop\temp

r"C:\Users\is0009\Desktop\temp\Rural_OCP_map_1_17x11.mxd"
r"C:\Users\is0009\Desktop\temp\Rural_OCP_map_2_17x11.mxd"
r"C:\Users\is0009\Desktop\temp\Rural_OCP_map_3_17x11.mxd"
r"C:\Users\is0009\Desktop\temp\Rural_OCP_map_4_17x11.mxd"
r"C:\Users\is0009\Desktop\temp\Rural_OCP_map_5_17x11.mxd"

...where I am trying to pass the above 'string' as my MapDocument.


But when I modify the script to then use that string:
#Import python modules
import os, sys, string, glob, arcpy.mapping

#Set local variables
mxdLst = glob.glob('*.mxd')
mxdCnt = len(mxdLst)

####
#Place script in same folder as MXDs to get 'current working directory'
####
baseF = os.getcwd()

#Print how many mxd's found
print '\n' + 'Found ' + str(mxdCnt) + ' MXDs for exporting to PDF...'
print 'Directory: ' + baseF + '\n'


#For loop to process each mxd into a PDF
for mxds in mxdLst:
    outPDF = mxds.replace('mxd', 'pdf') #Replace 'mxd' extension with 'pdf'
    print 'Exporting ' + mxds + ' -> ' + outPDF
    mxd = 'r' + '\"' + baseF + '\\' + mxds + '\"' #mxd with fullpathname
    inMxd = arcpy.mapping.MapDocument(mxd) #Make current mxd in loop the mapdocument
    arcpy.mapping.ExportToPDF(inMxd, outPDF) #Export mapdocument to pdf
    print 'Done exporting: ' + outPDF
    #print mxd
del mxds



Results in the following error from IDLE now:
Found 5 MXDs for exporting to PDF...
Directory: C:\Users\is0009\Desktop\temp

Exporting Rural_OCP_map_1_17x11.mxd -> Rural_OCP_map_1_17x11.pdf
Traceback (most recent call last):
  File "C:\Users\is0009\Desktop\temp\mxdloop.py", line 23, in <module>
    inMxd = arcpy.mapping.MapDocument(mxd) #Make current mxd in loop the mapdocument
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\arcobjects\mixins.py", line 443, in __init__
    assert (os.path.isfile(mxd) or (mxd.lower() == "current")), gp.getIDMessage(89004, "Invalid MXD filename")
AssertionError: Invalid MXD filename.





What is it that is making my MXD name invalid?

Is it because I am not handling the slashes in the filepath name correctly and they are considered invalid characters?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Try the following code:

import arcpy, glob, os

#Place script in same folder as MXDs to get 'current working directory'
baseF = os.getcwd()
print baseF

#Set local variables
mxdLst = glob.glob(baseF + '\\' + '*.mxd')
mxdCnt = len(mxdLst)

#Print how many mxd's found
print '\n' + 'Found ' + str(mxdCnt) + ' mxds for PDF exporting...'
print 'Directory: ' + str(baseF) + '\n'

#Loop to process each mxd into a PDF
for mxds in mxdLst:
    PDFr = mxds.replace('mxd', 'pdf') #Replace 'mxd' extension with 'pdf'
    print 'Exporting ' + str(mxds) + ' to:' + '\n' + str(baseF) + '\\' + str(PDFr) #Print current mxd exporting and output pdf name...
    inMxd = arcpy.mapping.MapDocument(mxds) #Make current mxd in loop the mapdocument
    arcpy.mapping.ExportToPDF(inMxd, PDFr) #Export mapdocument to pdf
    print 'Done exporting: ' + str(PDFr)
del mxds


I specified 'baseF = os.getcwd()' first and then added the baseF variable to the glob module, 'mxdLst = glob.glob(baseF + '\\' + '*.mxd')'.
0 Kudos
DonovanCameron
Occasional Contributor II
I specified 'baseF = os.getcwd()' first and then added the baseF variable to the glob module, 'mxdLst = glob.glob(baseF + '\\' + '*.mxd')'.


Thanks for the simplification! It throws the old error where the connection was closed...

But when I make the following change (surround in double quotes):
    inMxd = arcpy.mapping.MapDocument('\"' + mxds + '\"') #Make current mxd in loop the mapdocument

or
(surround in double quotes with 'r' prefix)
    inMxd = arcpy.mapping.MapDocument('r\"' + mxds + '\"') #Make current mxd in loop the mapdocument


I get the invalid mxd name error from pyscripter (it crashed in IDLE):
[ATTACH]7948[/ATTACH]
Looks like the part in red shows it is NOT passing the os.path.isfile(mxd) check/test.


Side question: When I use the 'str' command in the script is it not necessary to import the 'string' module?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You will not have to add quotes to the 'mxds' variable.  I believe because you are looping through a list that is comprised of strings, and therefore python knows the variable 'mxds' is already a string.  Example, you could simply write:

for mxds in mxdLst:
    print mxds + " is a map document"


You would not have to write:

for mxds in mxdLst:
    print str(mxds) + " is a map document"


In your code, try:

inMxd = arcpy.mapping.MapDocument(mxds) #Make current mxd in loop the mapdocument
0 Kudos
DonovanCameron
Occasional Contributor II
Thanks for all your help.

This script started from the simplest form as taken from the bottom of this page:
http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/ExportToPDF/00s300000027000000/

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\Users\is0009\Desktop\temp\Rural_OCP_map_1_17x11.mxd")

print 'Exporting PDF...'
arcpy.mapping.ExportToPDF(mxd, r"C:\Users\is0009\Desktop\temp\Rural_OCP_map_1_17x11.pdf")
print 'Done exporting!'

del mxd


The above fails with my original error: An existing connection was forcibly closed by the remote host.
My license level is ArcView (local) and occasionally I use an ArcEditor concurrent-use when needed.

This is running on a fresh install of windows 7 and arcgis 10 sp2.

Not tested with sp1 or no sp installed.

Windows 7 firewall is turned off.


EDIT:
I have tried using all forms of path names from the help: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Setting_paths_to_data_in_Python/002z00...
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Does your data within your MXDs resides within an SDE geodatabase?  If so, can you try this script on an MXD that contains data from a File or Personal geodatabase?
0 Kudos
DonovanCameron
Occasional Contributor II
Does your data within your MXDs resides within an SDE geodatabase?  If so, can you try this script on an MXD that contains data from a File or Personal geodatabase?


EDIT:
Forgot to say that my 'rocp' mxds do have data from shapefiles, gdb, and an sde.


I created some new/blank mxds that all exported fine:
Shapefiles.mxd
FileGDB.mxd
SDE.mxd
All3.mxd

Its my "rocp" mxds that fail! They were from 9.3.1 so I opened one and saved it as a copy to version 10; still failed.


" rel="nofollow" target="_blank">http://www.youtube.com/watch?v=S8iclZrdp2A[/video]
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Let's try recreating the MXD.  Open the corrupted MXD and select all the layers within the Table of Contents > right-click on one of the selected layers > Copy.  Next, start a new MXD and right-click on the data frame > Paste Layers.  Save this MXD with a new name and try exporting to a PDF using your script.  Do you still receive the same error?
0 Kudos
DonovanCameron
Occasional Contributor II
Let's try recreating the MXD.


That will work, but it may not be feasible given the production method used for these mxd's...

I can for example, copy and paste all page layout elements (dataframes, scalebars, images, north arrows and legends with a Ctrl+A, Ctrl+C then a Ctrl+V) successfully into a new blank mxd. But their position and size are lost, sometimes larger or smaller depending on the viewing scale in my layout view. I cannot let this happen, because now I have to go and match the size and position of EVERY layout element to the old values.

(I tried grouping everything and then batch re-sizing this single object but some objects don't maintain their respective positions)

Making sure both mxd's are 1:1 in layout mode will help get rid of some of the scaling issue...
Reference scales are kept, but the map extent changes.

The formatting in my legends are lost too, they paste fine and at first - visually they are correct, but when I save and close then re-open the mxd, they default to a "default legend" will ALL the layers in my TOC present, with default symbol/patch properties and export to PDF as such.

My formatting and styles are not kept in the legend...

I have to copy and paste the actual dataframes because they contain annotation groups that need to be carried over.

I would like to batch export these because it takes me half a working day just to open > export map > close > open next map...

Any thoughts on if I should be 'upgrading' to 9.3.1?


I really enjoy the new look and feel of v10 but some of these minor hiccups cut deep into my day(s) and confuse me especially when I am trying to learn more about arcgis python scripting.

This reminds me of the time when I was younger and went to the hospital to get my cast removed. I was terrified of the doctor with that vibrating saw thingy.

"It won't even cut through paper she says", and places a piece of paper on the bench and turns the saw on at which point it cuts through cleanly.

"Bad example", is all she said and quickly went to work on removing my cast...
0 Kudos