Select to view content in your preferred language

Attach pdf to points

1472
5
09-15-2022 11:52 AM
arifkhan
New Contributor

Hello,

I am new to python in ArcGIS pro and I am trying to automate our workflow.

I want to upload pdf attachments to more than 500 points - doing this manually takes hours and hours of work. I need help with writing a code that selects these 500 points and upload the pdf attachments that I have on my computer. 

The pdf file name and one of the attributes of the point layer ("Name") are the same. For example, if the one of the points is named “HH NWLD 1.2-3.4" I would like this code to attach the pdf called "HH NWLD 1.2-3.4" from my machine.

Is this something possible? Can a code attach pdf files to a feature? I appreciate your input on this.

Thanks,

 

Tags (3)
0 Kudos
5 Replies
RhettZufelt
MVP Notable Contributor

You may want to look at the attachment toolset.  You can generate a match table with the point name/pdf path/name and feed that to the add attachments tool to accomplish this.

R_

 

0 Kudos
RogerDunnGIS
Frequent Contributor

One thing I experienced with AddAttachments was, I had fixed some file paths to about 20 records, selected those records and reran the tool.  However, AddAttachments appeared to have ignored my selection and reattached all 12,000 of the connected files to my feature class.  Not fun.  I wondered why that particular tool worked differently.

0 Kudos
by Anonymous User
Not applicable

Also you need to consider if you want to attach the pdf (makes a copy and stores it in the db) using the attachment toolset that Rhett mentioned, or attach a link to the pdf and use a web shared folder/ shared network folder that people can get to.

What are your storage options for this shapefile?

0 Kudos
arifkhan
New Contributor

I would like to store the pdf in the gdb.

I have enabled the attachment for the point layer, so I am trying to figure out the matching attachment table.

0 Kudos
by Anonymous User
Not applicable

Edit to remove some info that did pertain to updating attachments.

You can give this a try:

 

from os import listdir
from os.path import isfile, join
import csv
import arcpy

fc = r'path to the attachments fc'
pdfDir = r'path to pdf directory'

# create a dictionary of pdf files.
pdffiles = {f: {'filePdf': f.split('.')[0], 'filePdfName': f, 'pdfPath': join(pdfDir, f)} for f in listdir(pdfDir) if all([isfile(join(pdfDir, f)), f.endswith('pdf')])}

# add the matching OBJECTID from the featureclass:
# make list of pdf names for the sql query.
pdfs = [k for k in pdffiles.keys()]
# limit the results to the pdfs in the folder and add the objectID to the dictionary.
sql = f"""attach_name IN ({str(pdfs)[1:-1]})"""
with arcpy.da.SearchCursor(fc, ['OBJECTID', 'attach_name'], sql) as sCur:
    for row in sCur:
        pdffiles[row[1]]['OBJECTID'] = row[0]

# convert the pdf file dict to a list of dicts containing the matched objectid and pdf data.
dict_data = [dict(v.items()) for k, v in pdffiles.items()]

# fields to use as the csv header
csv_header = ['OBJECTID', 'filePdf', 'filePdfName', 'pdfPath']

# write the csv
with open(r'match.csv', 'w', newline="") as csvfile:
    writer = csv.DictWriter(csvfile, fieldnames=csv_header)
    writer.writeheader()
    for data in dict_data:
        writer.writerow(data)

# add the attachments
arcpy.management.AddAttachments(fc, 'OBJECTID', 'match.csv', 'OBJECTID', 'pdfPath')