Exportreport with image included in pdf

1072
5
Jump to solution
10-12-2016 07:38 AM
kyleturner
New Contributor III


I'm trying to write a script to export a report that includes a map in the output pdf. While the script will create the pdf report and a separate .emf file, the script won't include the emf image in the pdf document. I have copy/pasted the example from the Desktop help (search for ExportReport to see to what I am referring to) with only minor edits to the script. See below:

import arcpy, os
path = os.getcwd() #a relative path allowing for easy packaging
#Create PDF and remove if it already exists
pdfPath = path + r"\States_SubRegions.pdf"
if os.path.exists(pdfPath):
     os.remove(pdfPath)
pdfDoc = arcpy.mapping.PDFDocumentCreate(pdfPath)
subRegionList = ["744","760"]
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd)[0]
lyr = arcpy.mapping.ListLayers(mxd, "BuildingSpace_A")[0]
pageCount = 1
for region in subRegionList:
  #Generate image for each sub region
## whereClause = "facilityID = '" + region + "'"
## lyr.definitionQuery = whereClause
## arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)
## df.extent = lyr.getSelectedExtent()
## arcpy.SelectLayerByAttribute_management(lyr, "CLEAR_SELECTION"
   arcpy.mapping.ExportToBMP(mxd, path + "\RegionalPicture.bmp", df) #single file
#Generate report
   arcpy.mapping.ExportReport(report_source=lyr, report_layout_file=path + r"\custodial_areas.rlf", output_file=path + r"\temp" + str(pageCount) + ".pdf", dataset_option="USE_RLF",starting_page_number=pageCount)
#Append pages into final output
   pdfDoc.appendPages(path + r"\temp" + str(pageCount) + ".pdf")
   #os.remove(path + r"\temp.pdf")
   pageCount = pageCount + 1
pdfDoc.saveAndClose()
del mxd 

Maybe I don't understand the purpose of the script??

Your help is greatly appreciated and thanks in advance for any help.

Cheers


I'm using v. 10.3.1 for Desktop

32 bit windows with 32 bit ESRI

0 Kudos
1 Solution

Accepted Solutions
kyleturner
New Contributor III

For anybody interested, I figured out how to complete my task.

First my previous response didn't quite solve my problem. It created a separate map layout page then a separate report page which then combined into a two page document. However, I wanted the report to be on the same layout page. So my solution was the following:

I used ExportReport to create the report as a tif image. Then, with an existing picture element on the page layout, I simply remapped the picture source to the newly created tif source path. Then I simply exported the page layout with the included report to a pdf document (of course this can be included in a loop for multiple areas/items...which is in fact what I did).

However, there is one caveat....when the code attempts to remove all the temporary tifs I get the error that the last tif is in use because, it is in fact in use. Yet, my task is complete because the pdf of the page layout with the report included was successfully created. (Poor programming I know, but I'm not a coder and it does what I need it to do.)

If anyone is interested in seeing the code I am more than glad to provide it.

Cheers.

View solution in original post

0 Kudos
5 Replies
DanPatterson_Retired
MVP Emeritus

could you please format the code so it is readable Kyle

/blogs/dan_patterson/2016/08/14/script-formatting 

0 Kudos
kyleturner
New Contributor III

Dan,

Sorry about the formatting. I couldn't figure it out....I thought that was what the <code></code> tags were for.

Thanks for the link.

0 Kudos
kyleturner
New Contributor III

Correction:

I'm using 64 bit windows 7

0 Kudos
kyleturner
New Contributor III

For whatever unknown reason I got a more complex script to work. However, instead of exporting to emf, I exported the page layout to pdf and then combined that with the report pdf.

0 Kudos
kyleturner
New Contributor III

For anybody interested, I figured out how to complete my task.

First my previous response didn't quite solve my problem. It created a separate map layout page then a separate report page which then combined into a two page document. However, I wanted the report to be on the same layout page. So my solution was the following:

I used ExportReport to create the report as a tif image. Then, with an existing picture element on the page layout, I simply remapped the picture source to the newly created tif source path. Then I simply exported the page layout with the included report to a pdf document (of course this can be included in a loop for multiple areas/items...which is in fact what I did).

However, there is one caveat....when the code attempts to remove all the temporary tifs I get the error that the last tif is in use because, it is in fact in use. Yet, my task is complete because the pdf of the page layout with the report included was successfully created. (Poor programming I know, but I'm not a coder and it does what I need it to do.)

If anyone is interested in seeing the code I am more than glad to provide it.

Cheers.

0 Kudos