I've searched the forums but I can't find any python examples to change field order for layers in the ArcGIS Desktop 10.6 Table of Contents. Is there another way to automate this process?
I'm sorry, I guess I got my wires crossed when I read "...order for layers in the ArcGIS Desktop 10.6 Table of Contents". Now I understand what you really would like to do. I just googled 'arcpy reorder fields'. Give that a shot: there are several returns.
Hi Joe,
I looked at the links and I not quite sure if those links are able to rearrange fields in the layer properties. I am importing several FC layers into a fresh map document and I was hoping to change the attribute field order in which they will appear. I know we can change them manually but I was hoping there would be a way in python to rearrange the field order and save the map document, similar to importing FC layers and saving. I'll provide the code and image below.
<SPAN class="keyword token">import</SPAN> os <SPAN class="keyword token">import</SPAN> arcpy <SPAN class="comment token"># if outside ArcMap</SPAN> <SPAN class="comment token"># inside ArcMap, use "CURRENT" for document name</SPAN> mxd <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>MapDocument<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">"C:\Users\jnmiller\Desktop\Rebuild Map py Scripts\Untitled.mxd"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Input Variables</SPAN> <SPAN class="comment token">#</SPAN> path <SPAN class="operator token">=</SPAN> r<SPAN class="string token">"C:\Users\jnmiller\AppData\Roaming\ESRI\Desktop10.6\ArcCatalog\SDE.sde"</SPAN> <SPAN class="comment token"># path source w/layers</SPAN> df <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListDataFrames<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"*"</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">[</SPAN><SPAN class="number token">0</SPAN><SPAN class="punctuation token">]</SPAN> <SPAN class="comment token"># Add layer to TOC</SPAN> <SPAN class="comment token">#</SPAN> fullPath <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"ProposedMainlineProjects\ProposedMainlineProjects"</SPAN><SPAN class="punctuation token">)</SPAN> addLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>Layer<SPAN class="punctuation token">(</SPAN>fullPath<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>AddLayer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> addLayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BOTTOM"</SPAN><SPAN class="punctuation token">)</SPAN> fullPath <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"P_PipeSystem\P_Meters"</SPAN><SPAN class="punctuation token">)</SPAN> addLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>Layer<SPAN class="punctuation token">(</SPAN>fullPath<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>AddLayer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> addLayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BOTTOM"</SPAN><SPAN class="punctuation token">)</SPAN> fullPath <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"P_PipeSystem\P_MeterSetting"</SPAN><SPAN class="punctuation token">)</SPAN> addLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>Layer<SPAN class="punctuation token">(</SPAN>fullPath<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>AddLayer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> addLayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BOTTOM"</SPAN><SPAN class="punctuation token">)</SPAN> fullPath <SPAN class="operator token">=</SPAN> os<SPAN class="punctuation token">.</SPAN>path<SPAN class="punctuation token">.</SPAN>join<SPAN class="punctuation token">(</SPAN>path<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"P_PipeSystem\P_Regulator"</SPAN><SPAN class="punctuation token">)</SPAN> addLayer <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>Layer<SPAN class="punctuation token">(</SPAN>fullPath<SPAN class="punctuation token">)</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>AddLayer<SPAN class="punctuation token">(</SPAN>df<SPAN class="punctuation token">,</SPAN> addLayer<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"BOTTOM"</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># Set Visable = False</SPAN> <SPAN class="comment token">#</SPAN> <SPAN class="keyword token">for</SPAN> lyr <SPAN class="keyword token">in</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>mapping<SPAN class="punctuation token">.</SPAN>ListLayers<SPAN class="punctuation token">(</SPAN>mxd<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN> <SPAN class="keyword token">print</SPAN> lyr<SPAN class="punctuation token">.</SPAN>name lyr<SPAN class="punctuation token">.</SPAN>visible <SPAN class="operator token">=</SPAN> <SPAN class="token boolean">False</SPAN> mxd<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="keyword token">del</SPAN> mxd<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
You'll probably need to take a look here to begin with: MoveLayer—Help | ArcGIS Desktop
While your at it, get familiar with: MapDocument—Help | ArcGIS Desktop & ListLayers—Help | ArcGIS Desktop
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.