<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: legend wont adjust, mxd wont save from script in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329053#M25585</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I tested the following code using your MXD.&amp;nbsp; I tried loading the code into the Python window and running it as a script tool from within ArcMap and it works just fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also ran this as a stand-alone script using the full MXD path and that worked too.&amp;nbsp; All resulting MXDs opened as expected.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\correctly_saved.mxd")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you try the tests above?&amp;nbsp; If you can't repro with these simple scipts, then the issue is somewhere else in your code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sat, 11 Dec 2021 15:35:20 GMT</pubDate>
    <dc:creator>JeffBarrette</dc:creator>
    <dc:date>2021-12-11T15:35:20Z</dc:date>
    <item>
      <title>legend wont adjust, mxd wont save from script</title>
      <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329050#M25582</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello - this is another situation where I can perform all of the tasks from the python window, but not from a script.&amp;nbsp; The legend is supposed to turn into 2 columns at the end, but remains as 1.&amp;nbsp; Then, the .mxd saves, but the "FLUM_Select layer disappears, as does its parent layer, which is expected.&amp;nbsp; Ive attached the resulting mxd files.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I would much prefer to run this entirely from python, without having to go to Arcmap first and run it, but that has never completely worked either.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for your help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Paul Frank&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;City of Austin&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;# ------------------------------------------------------------------------------
# Name:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; make_plan_amendment_map.py
# Purpose:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Make and print a plan amendment map.&amp;nbsp; The user
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; enters a case number, and the script would zoom the map to that
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case and fill out the title and other map elements based on the
#&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; case information.
# Created on:&amp;nbsp;&amp;nbsp; December 20, 2011
# By:&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Paul Frank
# ------------------------------------------------------------------------------

# Import system modules
import arcpy
from arcpy import env
import os
env.overwriteOutput = True

#declare setup variables
inworkspace = r"G:\NEIGHBOR PLAN\ArcView\Projects\Templates\\" #declare workspace variable
incasenum = "NPA-2010-0012.01" #case number to center map
qyrstring = "\"CASENUM\" = " + "'" + incasenum + "'" #string to create definition query
env.workspace = inworkspace #set workspace
mxd = arcpy.mapping.MapDocument("CURRENT") #set mxd to current map

#create definition query for case and zoom to that polygon
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] #set dataframe object
flumlyr = arcpy.mapping.ListLayers(mxd, "FLUM", df)[0] #set layer object for selection target
zcaselyr = arcpy.mapping.ListLayers(mxd, "ZoningCases", df)[0] #set layer object to select from
zcaselyr.definitionQuery = qyrstring #create definition query for centering map
df.extent = zcaselyr.getSelectedExtent(False)
df.scale = 2000

#select nearby features to draw in legend
arcpy.SelectLayerByLocation_management(flumlyr, "WITHIN_A_DISTANCE", zcaselyr, "2000 Feet", "NEW_SELECTION") #select nearby features
outputlyr = "FLUM_Select" #declare variable to add selected features to display
arcpy.MakeFeatureLayer_management(flumlyr, outputlyr) #make feature layer from selection
arcpy.SetParameterAsText(0, outputlyr) #add to display.&amp;nbsp; Also set up output in add script wizard

#adjust symbology and legend
arcpy.ApplySymbologyFromLayer_management("FLUM_Select", flumlyr) #use symbology from parent layer
arcpy.mapping.RemoveLayer(df, flumlyr) #remove parent layer
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT", "Legend")[0] #set legend object
legend.adjustColumnCount(2) #adjust legend object

#refresh display and save mxd
#arcpy.RefreshActiveView() #refresh view
mxd.saveACopy(r"G:\NEIGHBOR PLAN\ArcView\Projects\Templates\test2.mxd") #save to another file
del mxd&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 03 Jan 2012 21:30:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329050#M25582</guid>
      <dc:creator>PaulFrank</dc:creator>
      <dc:date>2012-01-03T21:30:36Z</dc:date>
    </item>
    <item>
      <title>Re: legend wont adjust, mxd wont save from script</title>
      <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329051#M25583</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you are running this script outside of an ArcMap session, it will fail due to the fact that "CURRENT" only works when inside arcmap.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Try changing the following:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd = arcpy.mapping.MapDocument("CURRENT") #set mxd to current map&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;to the path of the arcmap document:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd = arcpy.mapping.MapDocument(r"c:\temp\mymapdoc.mxd") #set mxd to current map&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope this helps.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Jan 2012 15:39:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329051#M25583</guid>
      <dc:creator>AndrewChapkowski</dc:creator>
      <dc:date>2012-01-04T15:39:07Z</dc:date>
    </item>
    <item>
      <title>Re: legend wont adjust, mxd wont save from script</title>
      <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329052#M25584</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;If you are running this script outside of an ArcMap session, it will fail due to the fact that "CURRENT" only works when inside arcmap.&amp;nbsp; &lt;BR /&gt;&lt;BR /&gt;Try changing the following:&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd = arcpy.mapping.MapDocument("CURRENT") #set mxd to current map&lt;/PRE&gt;&lt;BR /&gt;to the path of the arcmap document:&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;mxd = arcpy.mapping.MapDocument(r"c:\temp\mymapdoc.mxd") #set mxd to current map&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Hope this helps.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Chris,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the reply.&amp;nbsp; You may have given me some ideas, but I understand that "CURRENT" only works in an active Arcmap session, which is in fact where I run this script, via Arctoolbox after adding the script there.&amp;nbsp; Again, everything works except the saveACopy and legend.adjustColumnCount(2) code.&amp;nbsp; But id appreciate additional suggestions.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 04 Jan 2012 21:45:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329052#M25584</guid>
      <dc:creator>PaulFrank</dc:creator>
      <dc:date>2012-01-04T21:45:45Z</dc:date>
    </item>
    <item>
      <title>Re: legend wont adjust, mxd wont save from script</title>
      <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329053#M25585</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I tested the following code using your MXD.&amp;nbsp; I tried loading the code into the Python window and running it as a script tool from within ArcMap and it works just fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I also ran this as a stand-alone script using the full MXD path and that worked too.&amp;nbsp; All resulting MXDs opened as expected.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\correctly_saved.mxd")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can you try the tests above?&amp;nbsp; If you can't repro with these simple scipts, then the issue is somewhere else in your code.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:35:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329053#M25585</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2021-12-11T15:35:20Z</dc:date>
    </item>
    <item>
      <title>Re: legend wont adjust, mxd wont save from script</title>
      <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329054#M25586</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I tested the following code using your MXD.&amp;nbsp; I tried loading the code into the Python window and running it as a script tool from within ArcMap and it works just fine.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;mxd = arcpy.mapping.MapDocument("CURRENT")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;I also ran this as a stand-alone script using the full MXD path and that worked too.&amp;nbsp; All resulting MXDs opened as expected.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Temp\correctly_saved.mxd")
legend = arcpy.mapping.ListLayoutElements(mxd, "LEGEND_ELEMENT")[0]
legend.adjustColumnCount(2)
mxd.saveACopy(r"C:\Temp\AdjustColumns.mxd")&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;Can you try the tests above?&amp;nbsp; If you can't repro with these simple scipts, then the issue is somewhere else in your code.&lt;BR /&gt;&lt;BR /&gt;Jeff&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Progress.&amp;nbsp; Yours worked.&amp;nbsp; Now mine works when loaded to the python window!&amp;nbsp; But it doesnt work when I run it from the Script tool.&amp;nbsp; "Always run in foreground" and "Run python script in process" are checked.&amp;nbsp; I agree its something above this code, but not finding where.&amp;nbsp; I have a feeling its how I am trying to select the layer by location, then add the result to the TOC.&amp;nbsp; I had to use an ESRI workaround to accomplish that - namely creating an output in the script tool.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 15:35:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329054#M25586</guid>
      <dc:creator>PaulFrank</dc:creator>
      <dc:date>2021-12-11T15:35:23Z</dc:date>
    </item>
    <item>
      <title>Re: legend wont adjust, mxd wont save from script</title>
      <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329055#M25587</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;If you send me a map package of an isolated area along with your script, I take a look at it.&amp;nbsp; Send to &lt;/SPAN&gt;&lt;A href="mailto:jbarrette@esri.com"&gt;jbarrette@esri.com&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Jan 2012 13:16:43 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329055#M25587</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2012-01-06T13:16:43Z</dc:date>
    </item>
    <item>
      <title>Re: legend wont adjust, mxd wont save from script</title>
      <link>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329056#M25588</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I think we have a fix.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.MakeFeatureLayer works well in CURRENT because it automatically adds the result to the current MXD.&amp;nbsp;&amp;nbsp; If you use a script tool or run as a stand alone script you need to modify your code as follows to make a layer object from MakeFeatureLayer and manage it accordingly:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;selLyr = arcpy.MakeFeatureLayer_management(flumlyr, "FLUM_Select").getOutput(0)&amp;nbsp; #added variable at beginning and .getOutput at end to return a real layer object&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.ApplySymbologyFromLayer_management(selLyr, flumlyr) #changed the first parameter&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;arcpy.mapping.AddLayer(df, selLyr) #new line - this added a real layer object (and also to the legend if the legend is set up to auto add new layers)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Jeff&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 11 Jan 2012 20:05:04 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/legend-wont-adjust-mxd-wont-save-from-script/m-p/329056#M25588</guid>
      <dc:creator>JeffBarrette</dc:creator>
      <dc:date>2012-01-11T20:05:04Z</dc:date>
    </item>
  </channel>
</rss>

