I know how to change settings of a legend individually by arcpy (like: Patch, Title, Items, Columns, Frame, Element Size & Position, etc.), but now I would like to use the exact legend settings another time and load them using ArcPy.

Is there a simple way to save/load these legend settings all together in/from a text file?

1 Answer 1


So it looks like you know how to get at the elements you need, and they are all read/write. I've done something similar where I read properties from a raster and write them out to a XML configuration file that in turn gets read by another Python script and the raster parameters get fed into a 3D modeling package. xml.etree.Elementree and xml.dom.minidom are the key modules (at least for my version).

First, read your legend elements and get the values into variables, then write them out to XML. Below, <..>_element each represent a element that I write out, where f is a file named Config.xml:

import xml.etree.ElementTree
import re

viesore_element = xml.etree.ElementTree.Element("VIESORE.Configuration")
comment = xml.etree.ElementTree.Comment("Auto-generated at " + str(write_time))
project_element = xml.etree.ElementTree.SubElement(viesore_element, 
project_element.text = proj_dir
scene_element = xml.etree.ElementTree.SubElement(viesore_element, 
# Write some more values out

# Get rid of extra line returns in prettified XML
uglyXml = prettify(viesore_element)
text_re = re.compile('>\n\s+([^<>\s].*?)\n\s+</', re.DOTALL)    
prettyXml = text_re.sub('>\g<1></', uglyXml)

f = open(os.path.join(proj_dir, filename), "w")

This gives me a XML file like so:

<?xml version="1.0" ?>
  <!-- Auto-generated at 11/14/2011 12:54:01 -->

I then read the XML in another application (this below needs cleaned up some, but it works):

import xml.dom.minidom

# Parse Config file
f = r'D:\Projects\VIESORE\Code\Python\Vue\Config.xml'
doc = xml.dom.minidom.parse(f)

image_dir_tag = doc.getElementsByTagName('ProjectDirectory')
if (len(image_dir_tag) == 1):
    working_dir = str(image_dir_tag[0].childNodes[0].toxml())

scene_tag = doc.getElementsByTagName('Scene')
if (len(scene_tag) == 1):
    scene = str(scene_tag[0].childNodes[0].toxml())

# Read some more elements in

Your Answer

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

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