Hello. Is it possible to run a batch export attachment script that exports based on a definition query assigned to the attachment table?
I was successful in running the script below. However, it exports all photos even though a definition query was applied to the table in the .mxd.
Any help is appreciated. Thank you for your time.
import arcpy
from arcpy import da
import os
inTable = arcpy.GetParameterAsText(0)
fileLocation = arcpy.GetParameterAsText(1)
with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID']) as cursor:
for item in cursor:
attachment = item[0]
filenum = "OBJ" + str(item[2]) + "_"
filename = filenum + str(item[1])
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())
del item
del filenum
del filename
del attachment
It doesn't work with Definition queries on the Feature Class, but a workaround is select by attributes--> Export attachments. Whoops, that behaviour is script-dependent, I think. Selecting narrows it down on a script that I've written but not on another one I tested.
Alternatively, you could come up a definition query on the Attachments table, but that's probably a lot more trouble than it's worth.
You'll need to put the definition query in the where_clause parameter spot in your search cursor for it to only get those attachments.
where_clause = 'your def query'
with da.SearchCursor(inTable, ['DATA', 'ATT_NAME', 'ATTACHMENTID'], where_clause) as cursor:
for item in cursor:
attachment = item[0]
filenum = "OBJ" + str(item[2]) + "_"
filename = filenum + str(item[1])
open(fileLocation + os.sep + filename, 'wb').write(attachment.tobytes())