English

How To: Batch export attachments from a feature class

Summary

There are no geoprocessing tools that allow users to export and save all attachments locally. The instructions provided below describe a possible solution.

The following script iterates through the entire attachment table of a single feature class and copies all of the attachments (saved as BLOBs, or binary large objects) to file.

Procedure

This script requires the input table to be the standard attachment table created when attachments are enabled on a feature class. This is because the below script relies on the DATA, ATT_NAME and ATTACHMENTID fields stored in this table. The typical naming convention should append _ATTACH to the end of the table name.

  1. Copy and paste the following script into Notepad and save it as ExportAttachments.py.
    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 = "ATT" + 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
  2. Right-click the toolbox and select Add > Script.
  3. Give the script a name and a label. Check the box next to Store relative path names, and click Next.
  4. For the script file, navigate to where the ExportAttachments.py script is saved. Select the script, click OK and click Next.
  5. Under Parameters, type Attachments Table under Display Name and set its data type as Table. Add a second parameter and type Output Location under display name and select Folder for its data type.
  6. Click Finish.
  7. A new script tool should now be added to the toolbox.
  8. Double-click the script tool. In the dialog, choose the attachments table that contains the attachments to be extracted for the first parameter. Choose a folder to save the exported attachments to under OutputLocation. Click OK to run the tool.