For future coders looking for "one" solution to this problem, I'm placing a short version of my final python solution.
Thanks to Heidi Kristenson for help with setting the thumbnail in the xml.
Problem: Update metadata on a geodatabase feature class from metadata maintained in a separate table. Include a thumbnail from an already created jpeg file.
Solution: Export the metadata from the target feature class to a scratch XML file. Modify the xml file elements with the new metadata values as well as the thumbnail. Import the modified metadata back into the target geodatabase feature class.
Of course, our solution iterates through a list of geodatabase feature classes to update the metadata which this sample does not illustrate for simplicity.
import os
import arcpy
import xml.etree.ElementTree as ET
import base64
from pyodbc import connect as odbcconn
from pandas import read_sql_query as readsqlqry
num_elements = 0
featName = 'your_featureClassName'
fcPathName = 'Database Connections/your_geodatabase.sde/' + featName
metaConnStr = 'DRIVER={SQL Server};SERVER=servername;DATABASE=dbname;Trusted_Connection=yes'
conn = odbcconn(metaConnStr)
metaqry = 'SELECT [FULL_NAME],[COVER_NAME],[ABSTRACT],[UPDATEDATE],[OWNERNAME]' +\
',[PATH],[METAACCESS],[ONMAINT],[MAINTFREQ],[KNOWNERROR],[LINEAGE]' +\
',[DOMAIN],[RECTIFIED],[MAINTORG],[MAINTDESC],[LIBINPUT],[SOURCNAME]' +\
',[SOURCCONTACT],[SOURCDOCNAME],[SOURCDATE],[SOURCSCALE],[SOURCFORMAT]' +\
',[SOUR2NAME],[SOUR2CONTACT],[SOUR2DOCNAME],[SOUR2DATE],[SOUR2SCALE]' +\
',[SOUR2FORMAT],[ONMG],[MGLAYERNAME],[MGSCALELOW],[MGSCALEHIGH] ' +\
'FROM [dbo].[metadata] WHERE [COVER_NAME] = \'' + featName + '\''
df_FCMeta = readsqlqry(metaqry, conn)
df_row = df_FCMeta.iloc[0]
arcpy.env.overwriteOutput = True
dir = arcpy.GetInstallInfo('desktop')['InstallDir']
copy_xslt = r'{0}'.format(os.path.join(dir,'Metadata\Stylesheets\gpTools\exact copy of.xslt'))
xmlfile = arcpy.CreateScratchName('.xml',workspace=arcpy.env.scratchFolder)
arcpy.XSLTransform_conversion(fcPathName, copy_xslt, xmlfile, '')
tree = ET.parse(xmlfile)
root = tree.getroot()
dataIdInfoEl = root.find('dataIdInfo')
subEl = ET.SubElement(dataIdInfoEl,'idPurp')
subEl.text = df_row['FULL_NAME']
num_elements += 1
subEl = ET.SubElement(dataIdInfoEl,'idAbs')
subEl.text = df_row['ABSTRACT']
num_elements += 1
thumbnailsPath = 'c:/thumbnails/'
jpgFile = thumbnailsPath + featName + '.jpg'
if os.path.exists(jpgFile):
with open(jpgFile, "rb") as img_file:
strEncoded = base64.b64encode(img_file.read())
attrib = {'EsriPropertyType':'PictureX'}
subEl = ET.SubElement(root,'Binary')
subEl = ET.SubElement(subEl,'Thumbnail')
subEl = ET.SubElement(subEl,'Data', attrib)
subEl.text = strEncoded
num_elements += 1
if num_elements > 0:
tree.write(xmlfile)
arcpy.MetadataImporter_conversion(xmlfile, fcPathName)
else:
print('No changes to save')