Batch Export Attachments Based On A Definition Query

319
2
01-26-2022 01:55 PM
CWH4
by
New Contributor

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

 

 

0 Kudos
2 Replies
AlfredBaldenweck
MVP Regular Contributor

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.

 

0 Kudos
by Anonymous User
Not applicable

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())

 

0 Kudos