1

I am having trouble debugging this error

`Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.5\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py", line 323, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python27\ArcGIS10.5\Lib\SITE-P~1\PYTHON~1\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python27\ArcGIS10.5\Lib\SITE-P~1\PYTHON~1\pywin\debugger\debugger.py", line 654, in run
    exec cmd in globals, locals
  File "C:\WORKSP~2\PYTHON~1\customPrint.py", line 249, in <module>
    getMap(templateMxd)
  File "C:\WORKSP~2\PYTHON~1\customPrint.py", line 180, in getMap
    getElements(templateMxdForPopup)
  File "C:\WORKSP~2\PYTHON~1\customPrint.py", line 191, in getElements
    txtElms = arcpy.mapping.ListLayoutElements(mapDoc, "TEXT_ELEMENT", "district_name")
  File "c:\program files (x86)\arcgis\desktop10.5\ArcPy\arcpy\utils.py", line 182, in fn_
    return fn(*args, **kw)
  File "c:\program files (x86)\arcgis\desktop10.5\ArcPy\arcpy\mapping.py", line 1547, in ListLayoutElements
    result = mixins.MapDocumentMixin(map_document).listLayoutElements(element_type, wildcard)
  File "c:\program files (x86)\arcgis\desktop10.5\ArcPy\arcpy\arcobjects\mixins.py", line 742, in listLayoutElements
    element_queue = [convertArcObjectToPythonObject(l) for l in self.elements]
  File "c:\program files (x86)\arcgis\desktop10.5\ArcPy\arcpy\arcobjects\mixins.py", line 763, in elements
    return self.pageLayout.elements
  File "c:\program files (x86)\arcgis\desktop10.5\ArcPy\arcpy\arcobjects\mixins.py", line 721, in pageLayout
    return convertArcObjectToPythonObject(self._mxd._arc_object.pageLayout)
AttributeError: 'unicode' object has no attribute '_arc_object'

The code is:

# location of ESRI mxd template in the server registered folder
#  Note: we can have two templates
# TODO remember to change this path to the server path
templatePath = r"C:\Workspace\pythonprint"

# logic to check the the Input Layout_Template
# if Layout_Template == "#" or not Layout_Template:
#     Layout_Template = "DDP_LayoutTemplate"
templateMxd = os.path.join(templatePath, Layout_Template + '.mxd')
def getMap(templateMxdForPopup):
    global mapDoc
    del mapDoc
    print "Map Doc will be:" + templateMxdForPopup
    mapDoc = arcpy.mapping.MapDocument(templateMxdForPopup)

    getElements(templateMxdForPopup)
#Convert the web map to a map document
result = arcpy.mapping.ConvertWebMapToMapDocument(Web_Map_as_JSON, templateMxd, noteGDB )
mapDoc = result.mapDocument

export(mapDoc, pdfPaths)
# resetting existing variables to point to elements from the new map
def getElements(mapDoc):
#access the elemenets in the map document and then delete any duplicates
global y
global routeName, districtName, scenarioName, scenarioNum, versionNum, author, date, northArrow
del y
del routeName, districtName, scenarioName, scenarioNum, versionNum, author, date, northArrow
y = topMargin
#text elements
txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "district_name")
if (txtElms.count > 0):
    districtName = txtElms[0]
    print districtName
    print txtElms
txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "scenario_name")
if (txtElms.count < 0):
    scenarioName = txtElms[0]
txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "version_number")
if (txtElms.count < 0):
    versionNum = txtElms[0]
txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "scenario_name")
if (txtElms.count > 0):
    scenarioName = txtElms[0]
txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "author")
if (txtElms.count > 0):
    author = txtElms[0]

#Text elements that are dynamic text
    txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "scenario_number")
if (txtElms.count > 0):
    scenarioNum = txtElms[0]
    txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "route_name")
if (txtElms.count > 0):
    routeName = txtElms[0]
    txtElms = arcpy.mapping.ListLayoutElements(mapDocBeingPassed, "TEXT_ELEMENT", "date")
if (txtElms.count > 0):
    date = txtElms[0]
    #graphic element
    graphicElm = arcpy.mapping.ListLayoutElements(mapDoc, "GRAPHIC_ELEMENT", "north_arrow")
    if (graphicElm.count > 0):
        northArrow = graphicElm[0]

I don't understand how to get rid of the unicode error to continue calling my getElement function.

1

In your getElements() function, you are running arcpy.mapping.ListLayoutElements(mapDoc, ...) where mapDoc should be an arcpy map document object. But you've passed it in as a text string (hence the 'unicode' error).

So replace this:

getElements(templateMxdForPopup)

with this:

getElements(mapDoc)

to pass in the map document object itself, instead of the path to the map document.

You've made the mapDoc variable a global in the getMap() function, but a parameter in the getElements() function. Mixing the same variable name between globals and local parameters should be avoided, because it's confusing and makes it difficult to determine which one is being used at some points in the script. Use parameters wherever possible, and avoid using globals unless absolutely necessary.

  • Thanks, I didn't see that! I am able to continue debugging other errors now. – Demon Sep 11 '18 at 22:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.