Select to view content in your preferred language

Open feature class attribute table with arcpy

1636
3
Jump to solution
02-23-2024 12:20 PM
BrandonMcAlister
Frequent Contributor

EDIT: To clarify some confusion. My script current adds EXTABLE, geocodes EXTABLE and saves a new feature class to a file geodatabase. That new Feature class is called EXAMPLE. I also want the script to open the attribute table in a new view window that is associated with EXAMPLE. 

The closest method I have found is .openView() under the Map class. I have not been able to successfully execute this method without receiving an error: openView() takes 1 positional argument but 2 were given. 

Sample:

aprx = arcpy.mp.ArcGISProject("Current")

MP = aprx.activeMap

Layer = MP.listLayers("EXAMPLE")[0]

MP.openView(Layer)

This returns the "openView() takes 1 positional argument but 2 were given error."

 

My next best guess it to just make python perform an action such as Ctrl + T or Ctrl double click to open the attribute table.

"I am once again asking for your support" - Bernie Sanders

 

I have a script that adds an access database table to a map.

Then geocodes that table and creates a new feature class.

I also want this script to open the attribute table associated with the new feature class. 

Is there a way to do this in arcpy?

Thanks,
Brandon
0 Kudos
1 Solution

Accepted Solutions
RHmapping
Occasional Contributor

Could not find an easy way to do this within arcpy.mp, however using a SendKeys style hack off of Stack Overflow you can send the Keyboard shortcut Ctrl+T right after adding the layer. See the code below, if you paste this directly into the Python window it doesn't work because it sends the keystrokes to the code editor, however if you put the script behind a Geoprocessing tool, it does work for me ..

import arcpy

# shamelessly lifted from ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# https://stackoverflow.com/questions/13289777/how-can-i-send-keyboard-commands-hold-release-simultanous-with-a-python-script
import ctypes, time

PUL = ctypes.POINTER(ctypes.c_ulong)

class KeyBdInput(ctypes.Structure) :
    _fields_ = [("wVk", ctypes.c_ushort), ("wScan", ctypes.c_ushort), ("dwFlags", ctypes.c_ulong), ("time", ctypes.c_ulong), ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong), ("wParamL", ctypes.c_short), ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long), ("dy", ctypes.c_long), ("mouseData", ctypes.c_ulong), ("dwFlags", ctypes.c_ulong), ("time",ctypes.c_ulong), ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput), ("mi", MouseInput), ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong), ("ii", Input_I)]

def doKey(hexKeyCode, keyUp=False) :
    direction_flag = 0
    if (keyUp) : direction_flag = 0x0002
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, direction_flag, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))    
    # ------------------------------------------------------------------------

def PressKeys(hexKeys, waitTime=1):
    for hk in hexKeys : doKey(hk)
    time.sleep(waitTime)
    for hk in hexKeys : doKey(hk, keyUp=True)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# --------------------------------------------------------------------------
try :
    proj = arcpy.mp.ArcGISProject("CURRENT")
    map = proj.activeMap
    lyr = arcpy.management.MakeFeatureLayer(r"C:\Users\rh\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\test_dataset", "TEST1234").getOutput(0)
    map.addLayer(lyr)
except Exception as ex :
    arcpy.gp.AddError(ex)

# https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
PressKeys( [0x11, 0x54], 1 ) # Ctrl + T

 

RHmapping_0-1709223931058.png

 

View solution in original post

0 Kudos
3 Replies
AlexanderDanielPratama
Esri Contributor

Hi, I am not sure about the question. Regarding access directly to the database, arcpy has arcpy.da (What is the Data Access module—ArcGIS Pro | Documentation). I usually use InsertCursor, UpdateCursor, and SearchCursor.

For geocodes, arcpy has geocode module (Geocode Addresses (Geocoding)—ArcGIS Pro | Documentation). Unfortunately, arcpy geocode best practice results only US. I already tried in my home country is not what I expected. Usually, I use another 3rd party such as the osm module. The result will be used by arcpy to record in the feature class or shp.

Hope, it might answered your curiosity

Cheers

If I'm reading your question correctly, you want the last step of the tool to open the attribute table the same as if you'd right-clicked on the layer and said "Open Attribute Table", correct?

Unless I'm mistaken, that would likely be in the arcpy.mp module, if it's anywhere.

 

I'm only just beginning most of my dive into that module, so I'm not familiar enough to say off-hand if/how it can be done.

------------------------------
M Reed
"The pessimist may be right oftener than the optimist, but the optimist has more fun, and neither can stop the march of events anyhow." — Robert A. Heinlein, in Time Enough for Love
0 Kudos
RHmapping
Occasional Contributor

Could not find an easy way to do this within arcpy.mp, however using a SendKeys style hack off of Stack Overflow you can send the Keyboard shortcut Ctrl+T right after adding the layer. See the code below, if you paste this directly into the Python window it doesn't work because it sends the keystrokes to the code editor, however if you put the script behind a Geoprocessing tool, it does work for me ..

import arcpy

# shamelessly lifted from ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
# https://stackoverflow.com/questions/13289777/how-can-i-send-keyboard-commands-hold-release-simultanous-with-a-python-script
import ctypes, time

PUL = ctypes.POINTER(ctypes.c_ulong)

class KeyBdInput(ctypes.Structure) :
    _fields_ = [("wVk", ctypes.c_ushort), ("wScan", ctypes.c_ushort), ("dwFlags", ctypes.c_ulong), ("time", ctypes.c_ulong), ("dwExtraInfo", PUL)]

class HardwareInput(ctypes.Structure):
    _fields_ = [("uMsg", ctypes.c_ulong), ("wParamL", ctypes.c_short), ("wParamH", ctypes.c_ushort)]

class MouseInput(ctypes.Structure):
    _fields_ = [("dx", ctypes.c_long), ("dy", ctypes.c_long), ("mouseData", ctypes.c_ulong), ("dwFlags", ctypes.c_ulong), ("time",ctypes.c_ulong), ("dwExtraInfo", PUL)]

class Input_I(ctypes.Union):
    _fields_ = [("ki", KeyBdInput), ("mi", MouseInput), ("hi", HardwareInput)]

class Input(ctypes.Structure):
    _fields_ = [("type", ctypes.c_ulong), ("ii", Input_I)]

def doKey(hexKeyCode, keyUp=False) :
    direction_flag = 0
    if (keyUp) : direction_flag = 0x0002
    extra = ctypes.c_ulong(0)
    ii_ = Input_I()
    ii_.ki = KeyBdInput( hexKeyCode, 0x48, direction_flag, 0, ctypes.pointer(extra) )
    x = Input( ctypes.c_ulong(1), ii_ )
    ctypes.windll.user32.SendInput(1, ctypes.pointer(x), ctypes.sizeof(x))    
    # ------------------------------------------------------------------------

def PressKeys(hexKeys, waitTime=1):
    for hk in hexKeys : doKey(hk)
    time.sleep(waitTime)
    for hk in hexKeys : doKey(hk, keyUp=True)
# ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

# --------------------------------------------------------------------------
try :
    proj = arcpy.mp.ArcGISProject("CURRENT")
    map = proj.activeMap
    lyr = arcpy.management.MakeFeatureLayer(r"C:\Users\rh\Documents\ArcGIS\Projects\MyProject\MyProject.gdb\test_dataset", "TEST1234").getOutput(0)
    map.addLayer(lyr)
except Exception as ex :
    arcpy.gp.AddError(ex)

# https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
PressKeys( [0x11, 0x54], 1 ) # Ctrl + T

 

RHmapping_0-1709223931058.png

 

0 Kudos