I was asked how to build pdfs from a script that would export map items and different layers with a legend.

First we have in ArcGIS client a bunch of layers that are called District_1, District_2,District_x,etc. These are File Geodatabase feature classes with geometry type Polygon.
Basically it's the poly that defines a border for a Congressional District.


We have a database table called Health Center Clinics, which has longitude, latitude, the district in which they fall in, name, parent Health Center Name, and misc information.

We want to: show the district poly, all the health center clinics within that district, change the text label in the pdf, and set the name of the pdf.

Desired Outcome: show poly for district 1, include all health centers clinics, change the name to show the Congress persons name, Make a legend show the Health Center parent.



import arcpy 
def PDFIt(dist):

	tname = "Unrepresented"
	tdis = dist
	fname = "Congressional District " + dist + " - Unrepresented";
	rows = arcpy.SearchCursor("Health Center Clinics",fields="us_house_rep;us_house_number")
	for row in rows:
		if row.getValue("us_house_number") == tdis:
			tname = "The Honorable " + row.getValue("us_house_rep") + '\n' + "Congressional District " + row.getValue("us_house_number")
			tempname = row.getValue("us_house_rep").replace(" ","_")
			tempname = tempname.replace(".","")
			tempname = tempname.replace(".","")
			tempname = tempname.replace("(D)","")
			tempname = tempname.replace("(R)","")
			tempname = str(tempname.encode("utf-8").replace("é","e").replace("í","i").replace("á","a"))
			tempname = str(tempname.encode("utf-8").replace("'",""))
			tempname = tempname.replace("__","_")
			fname = row.getValue("us_house_number") + "_mapc_" + tempname + ".pdf"
			
	print tname	
	print fname		
	mxd = arcpy.mapping.MapDocument("CURRENT")
	titleItem = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "*")[0]
	titleItem.text = tname
	df = arcpy.mapping.ListDataFrames(mxd)[0]  
	lyr = arcpy.mapping.ListLayers(mxd, 'District_' + tdis, df)[0] 
	ext = lyr.getExtent()  
	df.extent = ext  
	lyr.visible = True
	lyrm = arcpy.mapping.ListLayers(mxd, 'Health Center Clinics', df)[0]
	lyrm.visible = True
	lyrm.definitionQuery= "[is_advocacy]='1' and [status] in ('Active','Reportable') and [us_house_number]='" + dist + "'"
	arcpy.mapping.ExportToPDF(mxd, "D:\\dlfs\\us_congress_clinic_sitename\\" + fname)
	lyr.visible = False
	lyrm.definitionQuery= "[is_advocacy]='1'and [status] in ('Active','Reportable')"
	
for x in range(1, 37):
	PDFIt(str(x))