My preference is using 2nd dataframe, but this script will also work for basic tables. Just insert picture element and call it "myTABLE":
import matplotlib.pyplot as plt
import pandas as pd
# play with this
plt.figure(figsize=(8,1))
sourceFile = "f:/scratch/table.png"
tbl=arcpy.da.TableToNumPyArray("subc_5ha",("GRIDCODE","Shape_Area","SUBC_NAME","HA"))
df = pd.DataFrame(tbl)
data = df.values
plt.axis("off")
plt.table(cellText=data, colLabels=df.columns, loc="center",fontsize=8)
plt.savefig(sourceFile)
plt.close()
mxd = arcpy.mapping.MapDocument("CURRENT")
for elm in arcpy.mapping.ListLayoutElements(mxd, "PICTURE_ELEMENT"):
if elm.name == "myTABLE":
try:elm.sourceImage = ""
except: elm.sourceImage = sourceFile
Before:
After:
Script converts table to png raster/image and replaces source of the picture in existing picture. To make it work you need to change 3 lines of code:
plt.figure(figsize=(8,1))
these are dimensions (width =8', height = 1') of your table image.
sourceFile = "f:/scratch/table.png"
this is name of the image on disk.
tbl=arcpy.da.TableToNumPyArray("subc_5ha",("GRIDCODE","Shape_Area","SUBC_NAME","HA"))
first parameter here (subc_5ha) is name of the layer in the current mxd table of content, followed by list of fields (GRIDCODE,..HA) that you'd like to show. No Shape field though.
Add picture element to your layout and name it 'myTABLE':
Attach script to custom tool or run it from Python window. Check if table fits into element. If not modify figsize in line 1.
UPDATE:
It seems that using text element and prettytable Python module produces much nicer output:
Unfortunately ArcGis 10.8 doesn't want to work directly with that module, so workflow involves call to tiny external script. If of interest I can post it.