Select to view content in your preferred language

Comparing a Search Cursor list to number of PDFs Created.

6162
41
Jump to solution
03-01-2013 09:48 AM
MichelleCouden1
Deactivated User
I need some help on my program. I have the parameters set to ask user to pick an mxd and the folder where those data driven PDFs are located. I started with Search Cursor to create a list from the Sheet_ID field of an mxd. To get total number of pages of that mxd. Now I need to write in for the program to compare the list created from the Sheet numbers to the number of PDFs created and tell me if there is a difference in the total count. For Example: If there are 1 out of 11 sheets for the mxd, then there should be 11 PDFs created and so on. MY code is below:

#Purpose: Compares SheeID field to the toal PDFs created from Data Driven Pages Program

Import arcpy, os, string, sys

#Make parameters for people to choose mxd and folder of PDFs to compare
mxdList = string.split(arcpy.GetParameterAsText(0), ";")
dir = arcpy.GetParameterAsText(1)

#Use Search Cursor to go through Attribuite Table to get Sheet number info
rows = arcpy.SearchCursor(mxdList, "Sheet_ID")
                       
#Use list from Sheet_ID field to match the number of PDFs created to get difference
Tags (2)
0 Kudos
41 Replies
MichelleCouden1
Deactivated User
Problem! Can't have it read the layer inside the mxd because every grid layer is named differently. We have to have the user chose the feature class where it will find the Sheet_Id field to compare. Please check the code to see if I changed it correctly? Thanks!

import arcpy, os, sys
from os import path as p
from arcpy import mapping as m

#Make parameters for people to choose mxd and folder of PDFs to compare
mxdList = arcpy.GetParameterAsText(0).split(';')
pdf_path = arcpy.GetParameterAsText(1)
fc_path = arcpy.GetParameterAsText(2)

report = {}
page_list = []

#Use Search Cursor to go through Attribuite Table to get Sheet number info
for mapDoc in mxdList:
    mxd = arcpy.mapping.MapDocument(mapDoc)
    for lyr in m.ListLayers(mxd):
        if lyr.dataSource == fc_path:     (So here I would delete the if lyr.supports("Datasource) so that it doesn't try to
                                                                                find the layer correct?)
                max_list  = []
                rows = arcpy.SearchCursor(mxdList, "Sheet_ID")
                for row in rows:
                    max_list.append(row.Sheet_ID)
                sheet_count = max(max_list)
                arcpy.AddMessage('Sheet count: {0}'.format(sheet_count))
            else:
                arcpy.AddError('No Layers in %s match data source'%mapDoc)
pdfDoc = arcpy.mapping.PDFDocumentOpen(path)
print pdfDoc.pageCount
0 Kudos
by Anonymous User
Not applicable
Problem! Can't have it read the layer inside the mxd because every grid layer is named differently. We have to have the user chose the feature class where it will find the Sheet_Id field to compare. Please check the code to see if I changed it correctly? Thanks!

import arcpy, os, sys
from os import path as p
from arcpy import mapping as m

#Make parameters for people to choose mxd and folder of PDFs to compare
mxdList = arcpy.GetParameterAsText(0).split(';')
pdf_path = arcpy.GetParameterAsText(1)
fc_path = arcpy.GetParameterAsText(2)

report = {}
page_list = []

#Use Search Cursor to go through Attribuite Table to get Sheet number info
for mapDoc in mxdList:
    mxd = arcpy.mapping.MapDocument(mapDoc)
    for lyr in m.ListLayers(mxd):
        if lyr.dataSource == fc_path:     (So here I would delete the if lyr.supports("Datasource) so that it doesn't try to
                                                                                find the layer correct?)
                max_list  = []
                rows = arcpy.SearchCursor(mxdList, "Sheet_ID")
                for row in rows:
                    max_list.append(row.Sheet_ID)
                sheet_count = max(max_list)
                arcpy.AddMessage('Sheet count: {0}'.format(sheet_count))
            else:
                arcpy.AddError('No Layers in %s match data source'%mapDoc)
pdfDoc = arcpy.mapping.PDFDocumentOpen(path)

print pdfDoc.pageCount


Do all the layers in the mxd use the same grid?  I had it set up if all the grid layers use the same feature class, and it works in mine regardless of what the layer is called in the mxd.  If they all use different data sources it will need to be modified.
0 Kudos
MichelleCouden1
Deactivated User
Yeah, I checked all grids are named differently so I need the user to pick the feature class to run the search cursor. I am new to PYthon so Thank You very much for your help. I wanted to see if I could help with the changes. I've made some changes in the code, see if I'm right or I'm way off.

import arcpy, os, sys
from os import path as p

#Make parameters for people to choose mxd and folder of PDFs to compare
mxdList = arcpy.GetParameterAsText(0).split(';')
pdf_path = arcpy.GetParameterAsText(1)
fc_path = arcpy.GetParameterAsText(2)

report = {}
page_list = []

#Use Search Cursor to go through Attribuite Table to get Sheet number info
for mapDoc in mxdList:
    mxd = arcpy.mapping.MapDocument(mapDoc)
for fcClass in fc_path:
    fc = arcpy.ListFeatureClasses(fcClass)
            max_list = []
            rows = arcpy.SearchCursor(mxdList, "Sheet_ID")
            for row in rows:
                max_list.append(row.Sheet_ID)
                sheet_count = max(max_list)
                arcpy.AddMessage('Sheet count: {0}'.format(sheet_count))
            else:
                arcpy.AddError('No Layers in %s match data source'%mapDoc)
pdfDoc = arcpy.mapping.PDFDocumentOpen(path)
print pdfDoc.pageCount
0 Kudos
by Anonymous User
Not applicable
I think you are closer now.  But using the pdfDoc.pageCount is not necessary since your PDF's are only one page each.  That will only return 1 for each PDF.  I did a test map book and exported each page out to single pages and was able to get the below code to work. 

#Purpose: Compares SheeID field to the toal PDFs created from Data Driven Pages Program

import arcpy, os, sys
from os import path as p
from arcpy import mapping as m

# Make parameters for people to choose mxd and folder of PDFs to compare
mxdList = arcpy.GetParameterAsText(0).split(';')
pdf_path = arcpy.GetParameterAsText(1)
fc_path = arcpy.GetParameterAsText(2)

report = {}
for mapDoc in mxdList:
    arcpy.AddMessage(mapDoc)
    mxd = arcpy.mapping.MapDocument(mapDoc)
    for lyr in m.ListLayers(mxd):
        if lyr.supports('DATASOURCE'):
            if lyr.dataSource == fc_path:
                max_list = []
                rows = arcpy.SearchCursor(lyr)
                for row in rows:
                    max_list.append(row.Sheet_ID)
                sheet_count = max(max_list)
                arcpy.AddMessage('Sheet count:  %s'%sheet_count)
                print 'Sheet count:  %s'%sheet_count
            else:
                arcpy.AddError('No layers in %s match data source'%mapDoc)
                
    page_list = []     
    # List all PDF's in the pdf_path folder
    arcpy.env.workspace = ws = pdf_path
    for pdf_doc in arcpy.ListFiles('*.pdf'):
        pname = ''.join([a for a in pdf_doc.split('.')[0] if a.isalpha()])
        mname = p.basename(mapDoc.replace('_','')).split('.')[0]
        if pname == mname:
            page_list.append(int(''.join([i for i in pdf_doc.split('.')[0] if i.isdigit()])))
    page_count = max(page_list)
    arcpy.AddMessage('Page Count:  %s'%page_count)
    print 'Page Count:  %s'%page_count
    if page_count == sheet_count:
        arcpy.AddMessage('%s Sheet Count matches number of pages in %s' %(mapDoc,pdf_doc))
        print '%s Sheet Count matches number of pages in %s' %(mapDoc,pdf_doc)
    else:
        report[mapDoc] = pdf_doc

# Report maps that do not match up
if len(report) > 0:
    f = open(p.join(ws,'Page_Report.txt'),'w')
    for k,v in report.iteritems():
        f.write('"%s" Sheet Count does not match number of pages in "%s"\n\n' %(k,v))
    f.close()
    arcpy.AddMessage('Created report for map documents and PDF\'s')
    print 'Created report for map documents and PDF\'s'
else:
    arcpy.AddMessage('All PDF\'s have the correct amount of pages')
    print 'All PDF\'s have the correct amount of pages'



Since your PDF's are named like Abilene_Base_Map1, Abilene_Base_Map2, etc you will just want to extract the number that is on the PDF.  I then put those numbers in a list and use the max() function to find the maximum value (the last page of the PDF) and if that matches the max page number from the Sheet_ID field then it will tell you that there is the correct amount of pages.  If not, it will create a text file in the PDF folder that tells you which mxd and map book do not match up. The fc_path parameter is for the user to select the feature class that has the grid.

The text file had this output:

"C:\Users\GIS\Desktop\DDP\mxd\other_map.mxd" Sheet Count does not match number of pages in "other_map3.pdf"
0 Kudos
MichelleCouden1
Deactivated User
I think I am the one causing this error. Could it be how I have the parameters setup? Look at attached. It goes 12 times searching the mxd and says it can't find data source.  I'm puzzled because I am putting in the mxd, then putting in the PDF folder, and then putting in the feature class that has the field Sheet_ID. I think its me and my parameter setup.
0 Kudos
by Anonymous User
Not applicable
Hmmm I am not sure.  What are the input parameters you are using?  It looks like it only worked on one of them.  If all these maps use the same grid feature class it should work even if the layers are named differently in the map because it checks by data source and not layer name.  But if they all use a different grid feature class this will be tricky.
0 Kudos
MichelleCouden1
Deactivated User
The code should be fine. I have it where my parameters (listed in order) are set for the user to select one mxd (which is the one that does have the fc in it), then select the folder that holds all the pdfs that were created, and then last select the grid layout(feature class\Shapefile) for that mxd (that has the field Sheet_ID). Its wierd! It goes through some of the steps gives a sheet count right from the attribute table "Sheet count: Sheet 9 out of 11 Base Sheets" and then goes through a few more and then gives a page count: 11. I'll continue to look it over.
0 Kudos
MichelleCouden1
Deactivated User
So maybe that's it. I have to make my parameters for multiple mxds and all of their featureclass\shapefiles (multiple fc's). Now like I said before all maps grids have different names. But if we put in each feature class, it should work. Can't we make the code look through one feature class?
0 Kudos
by Anonymous User
Not applicable
Yes, that is possible.  Or one thing that may be easier, if you are able to make the grid layer the same name in all mxd's I would just do that.  Otherwise you could set the parameter to accept multi values and check if any of the layers in each mxd have a data source inside the list of grid feature classes.
0 Kudos
MichelleCouden1
Deactivated User
Can't change the name of the grid files because they are at different locations around the State of Texas. For Example: Houston has one that covers all its counties and then that tells us how many sheets (Data Driven Pages) are needed for that reference scale. If that makes sense. If the program though searches for the datasource, can I not have the program search the one featureclass\shapefile for the field Sheet_ID. Because, sheet_ID is in the feature class\shapefile. I'll try to figure out how to change that.
Also, will the program run on one mxd?
0 Kudos