Export attachment from database (Arcgis desktop 10.2)

5126
11
Jump to solution
07-12-2016 01:07 AM
BartoszBrzeziński1
New Contributor III

Hi everyone,

I would like to ask you about help. Maybe you have some solutions already. How could I export/extract few attachments for selected features from geodatabase? I tried use python scripts but no results. Thanks for help.

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Attached is a zip file that contains a toolbox with a GP tool you can run to export the attachments.

View solution in original post

11 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Bartosz,

Here is an example how to do this using python:

import os, arcpy

tbl = r"C:\Temp\Python\Test.gdb\Graffiti__ATTACH"

fldBLOB = 'DATA'

fldAttName = 'ATT_NAME'

outFolder = r"C:\Temp\Python\Attachments"

with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName]) as cursor:

   for row in cursor:

      binaryRep = row[0]

      fileName = row[1]

      # save to disk

      open(outFolder + os.sep + fileName, 'wb').write(binaryRep.tobytes())

print 'Finished'

BartoszBrzeziński1
New Contributor III

How should I excute it? I`m not good of using scripts.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

Attached is a zip file that contains a toolbox with a GP tool you can run to export the attachments.

View solution in original post

BartoszBrzeziński1
New Contributor III

Attachment.png

I get something like this.

0 Kudos
BartoszBrzeziński1
New Contributor III

I change in script "GlobalID" for "ATTACHMENTID" and it worked. Thank you Jake for your help.

JoeLivoti_II
New Contributor II

Hi Jake,

This script worked beautifully except for one thing that I need. Is there anyway to augment the script to change the name of the file to a parcel number in a related table or to include the parcel number in the attributes for the exported file?  Thank you for your help!

-Joe

0 Kudos
JakeSkinner
Esri Esteemed Contributor

If the parcel number is in the feature class, then this would easy to do simply by updating the SearchCursor.  If the parcel number is in a related table, it would require a lot more coding to obtain this value.  I found the following post that may be of some help:

https://community.esri.com/thread/88195 

0 Kudos
WilliamParco_GISP
New Contributor II

Hello Jake,

Sorry for reviving a dead post, but you mention that if the parcel number is in the feature class, then it would take a simple update to the SearchCursor to have the exported attachments be named with that number.

Would you be willing to elaborate on what needs changing? I've used this script many times with success, but I now have to sort my attachments by municipality and it would be great if I could just have them named with a field from the feature class and my coding skills are lacking.

Any advice on what needs changing would be very helpful.

0 Kudos
JakeSkinner
Esri Esteemed Contributor

William Parco GISP‌ you could do something like below:

import os, arcpy

fc = r"C:\PROJECTS\Collector\FieldData.gdb\ResidentialBuildings"
tbl = r"C:\PROJECTS\Collector\FieldData.gdb\ResidentialBuildings__ATTACH"
fldBLOB = 'DATA'
fldAttName = 'ATT_NAME'
attachRelate = 'REL_GLOBALID'
parcelField = 'OBJECTID'
parcelRelate = 'GlobalID'
outFolder = r"C:\Temp\Python\Attachments"

with arcpy.da.SearchCursor(tbl,[fldBLOB,fldAttName, attachRelate]) as cursor:
for row in cursor:
binaryRep = row[0]
with arcpy.da.SearchCursor(fc, [parcelField], "{0} = '{1}'".format(parcelRelate, row[2])) as cursor2:
for row2 in cursor2:
fileName = str(row2[0]) + ".jpg"
# save to disk
open(outFolder + os.sep + fileName, 'wb').write(binaryRep.tobytes())

print 'Finished'

attachRelate = foreign key in attachment table (used for relationship)

parcelField = field you want the attachment to be called once exported

parcelRelate = primary key in feature class (used for relationship)