Skip to content

Instantly share code, notes, and snippets.

@tjmichael81
Created December 16, 2013 05:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tjmichael81/7982540 to your computer and use it in GitHub Desktop.
Save tjmichael81/7982540 to your computer and use it in GitHub Desktop.
Export Esri geodatabase feature attachments with Python
def main():
pass
import arcpy
import os
#Script Variables
sourceAttachmentTable = r'C:\TEMP\Database.gdb\AttachmentTable__ATTACH'
targetOutputFolder = r'C:\TEMP\AttachmentExport'
filePathAttribute = 'FILE_PATH'
#Create a new column to contain the path to each exported file
if not filePathAttribute in arcpy.ListFields(sourceAttachmentTable):
arcpy.AddField_management(sourceAttachmentTable, filePathAttribute, 'TEXT', '', '', 200)
#Update the new column with the export path for each attachment
with arcpy.da.UpdateCursor(sourceAttachmentTable, [filePathAttribute, 'REL_OBJECTID', 'ATT_NAME']) as cursor:
for row in cursor:
row[0] = targetOutputFolder + os.sep + str(row[1]) + row[2]
cursor.updateRow(row)
del row
del cursor
#Export attachments from attachment table
with arcpy.da.SearchCursor(sourceAttachmentTable, [filePathAttribute, 'DATA']) as cursor:
for row in cursor:
outPath = row[0]
blobData = row[1]
open(outPath, 'wb').write(blobData.tobytes())
del row
del cursor
if __name__ == '__main__':
main()
@kelliek
Copy link

kelliek commented Nov 18, 2016

Thank you! This script saved me a crazy amount of time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment