Select to view content in your preferred language

Comparing a Search Cursor list to number of PDFs Created.

6188
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
Hey Caleb, need some help with sheet count!
0 Kudos
by Anonymous User
Not applicable
I have got the first part figured out, now working on the second part.  I have some variables at the top set for stand alone scripting.  The script tool variables are commented out:

import arcpy, os, sys, string
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
maps = r"C:\Users\GIS\Desktop\DDP\mxd\map_book.mxd;C:\Users\GIS\Desktop\DDP\mxd\other_map.mxd"
mxdList = maps.split(";")
pdf_path = r'C:\Users\GIS\Desktop\DDP'
##mxdList = arcpy.GetParameterAsText(0).split(";")
##pdf_path = arcpy.GetParameterAsText(1)

#Use Search Cursor to go through Attribuite Table to get Sheet number info

sheet_count = 0
for mapDoc in mxdList:
    arcpy.AddMessage(mapDoc)
    print mapDoc
    mxd = arcpy.mapping.MapDocument(mapDoc)
    max_list = []
    for lyr in m.ListLayers(mxd):
        try:
            rows = arcpy.SearchCursor(lyr)
            for row in rows:
                max_list.append(row.Sheet_ID)
            sheet_count = max(max_list)
            print 'Sheet Count:  %s' % sheet_count
            arcpy.AddMessage('Sheet Count:  %s' % sheet_count)
        except:
            pass
0 Kudos
MichelleCouden1
Deactivated User
So the top part still works, just still no print out of total number of sheets. I kept the mxd_list as GetParameterText because I need them to pick an mxd. Also, kept the (*,Grid) in the list layers because it is looking for a layer with Grid in the name of the mxd to get the sheet numbers. I wonder if it's not printing the sheet count because in the field of Sheet_ID the rows say "Sheet 1 of 11 Base Sheets".  

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)

#Use Search Cursor to go through Attribuite Table to get Sheet number info

sheet_count = ()

for mapDoc in mxdList:
    arcpy.AddMessage(mapDoc)
    print mapDoc
    mxd = arcpy.mapping.MapDocument(mapDoc)
    max_list = []
    for lyr in m.ListLayers(mxd, "*, Grid"):
        try:
            rows = arcpy.SearchCursor(lyr)
            for row in rows:
                max_list.append(row.Sheet_ID)
            sheet_count = max(max_list)
            print 'Sheet Count: %s' % sheet_count
            arcpy.AddMessage('Sheet count: %s'%sheet_count)
        except:
            pass

0 Kudos
by Anonymous User
Not applicable
Ok, I FINALLY figured it out, this was tricky!  So in my example I had 2 mxd's with data driven pages (9 Sheet_ID's each).  I then had 2 different sets of PDF'S.  I made one with the correct amount of pages (9) (matched up to 'map_book.mxd') and one with the wrong number of pages (3) which matched up to 'other_map.mxd'. I was able to get this to work:

import arcpy, os, sys, string
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)

#Use Search Cursor to go through Attribuite Table to get Sheet number info

sheet_count = 0
for mapDoc in mxdList:
    arcpy.AddMessage(mapDoc)
    mxd = arcpy.mapping.MapDocument(mapDoc)
    max_list = []
    for lyr in m.ListLayers(mxd):
        try:
            rows = arcpy.SearchCursor(lyr)
            for row in rows:
                max_list.append(row.Sheet_ID)
            sheet_count = max(max_list)
            print 'Sheet Count:  %s' % sheet_count
            arcpy.AddMessage('Sheet Count:  %s' % sheet_count)
        except:
            pass
    
    arcpy.env.workspace = ws = pdf_path
    for pdf_ in arcpy.ListFiles('*.pdf'):
        page_list = []
        pdf_list = []
        pdf = pdf_.split('.')[0].rstrip('0123456789')
        for doc in arcpy.ListFiles('%s*.pdf' % pdf):
            doc_ = doc.split('.')[0].rstrip('0123456789')
            if doc_ == p.basename(mapDoc).split('.')[0]:
                page_list.append(int(''.join([i for i in doc if i.isdigit()])))
                page_count = max(page_list)

    print 'PDF Page Count: %s' %page_count
    arcpy.AddMessage('PDF Page Count: %s' %page_count)
    if page_count == sheet_count:
        arcpy.AddMessage('\n"%s" Sheet Count matches number of pages in %s.pdf\n' %(p.basename(mapDoc),doc_))
        print '\n"%s" Sheet Count matches number of pages in %s.pdf\n' %(p.basename(mapDoc),doc_)
    else:
        arcpy.AddMessage('\n"%s" Sheet Count does not match number of pages in %s.pdf\n' %(p.basename(mapDoc),doc_))
        print '\n"%s" Sheet Count does not match number of pages in %s.pdf\n' %(p.basename(mapDoc),doc_)

[ATTACH=CONFIG]22440[/ATTACH][ATTACH=CONFIG]22441[/ATTACH][ATTACH=CONFIG]22442[/ATTACH]

EDIT:  I just noticed it does not print out the correct pdf name, but it does do the correct comparison.  I'm trying to figure that part out now.
0 Kudos
by Anonymous User
Not applicable
Try this:

import arcpy, os, sys, string
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)

#Use Search Cursor to go through Attribuite Table to get Sheet number info

# MXD LIST
mxd_dict ={}
for mapDoc in mxdList:
    arcpy.AddMessage(mapDoc)
    mxd = arcpy.mapping.MapDocument(mapDoc)
    max_list = []
    for lyr in m.ListLayers(mxd):
        try:
            rows = arcpy.SearchCursor(lyr)
            for row in rows:
                max_list.append(row.Sheet_ID)
            sheet_count = max(max_list)
            mxd_name = p.basename(mapDoc).split('.')[0]
            mxd_dict[mxd_name] = sheet_count
            
        except:
            pass
        
# PDF LIST
pdf_dict = {}      
pdf_list = []
arcpy.env.workspace = ws = pdf_path
for pdf_ in arcpy.ListFiles('*.pdf'):
    pdf_list.append(pdf_.split('.')[0].rstrip('0123456789'))
unique = list(set(pdf_list))

for pdf  in unique:
    for doc in arcpy.ListFiles('%s*.pdf' % pdf):
        page_list = []
        doc_ = doc.split('.')[0].rstrip('0123456789')
        for key in mxd_dict.keys():
            if doc_ == key:
                page_list.append(int(''.join([i for i in doc if i.isdigit()])))
                page_count = max(page_list)
            pdf_dict[doc_] = page_count

# REPORT RESULTS
for pdf in unique:
    print 'MXD:\t%s\tSheet Count:\t%s'%(pdf, mxd_dict[pdf])
    arcpy.AddMessage('MXD:\t%s\tSheet Count:\t%s'%(pdf, mxd_dict[pdf]))

    print 'PDF:\t%s\tPage Count:\t%s'%(pdf, pdf_dict[pdf])
    arcpy.AddMessage('PDF:\t%s\tPage Count:\t%s'%(pdf, pdf_dict[pdf]))
    
    if mxd_dict[pdf] == pdf_dict[pdf]:
        arcpy.AddMessage('\n"%s.mxd" Sheet Count matches number of pages in %s.pdf\n' %(pdf,pdf))
        print '\n"%s.mxd" Sheet Count matches number of pages in %s.pdf\n' %(pdf,pdf)
    else:
        arcpy.AddMessage('\n"%s.mxd" Sheet Count does not match number of pages in %s.pdf\n' %(pdf,pdf))
        print '\n"%s.mxd" Sheet Count does not match number of pages in %s.pdf\n' %(pdf,pdf)




I believe this should achieve the results you want:
[ATTACH=CONFIG]22443[/ATTACH]
0 Kudos
MichelleCouden1
Deactivated User
i am getting a exceptions.KeyError'>:u'Amarillo_Base_Map'. Which is wierd where is it getting the u?
The error means this : exception KeyError
Raised when a mapping (dictionary) key is not found in the set of existing keys.

Could it be that there are 22 pdfs and the numbers in the program only go up to 9. So should I make them infinity because Houston has 154 pages.
0 Kudos
MichelleCouden1
Deactivated User
I did break point debugging. And I think I found where it stops. I did a breakpoint at mxd_dict = {} and at the beginning of each loop. I started with stepover and then hit Go. It went to the mxd_dict = {} and stopped and then I hit stepover and it went until it said Exception raised and it was at line 22. mxd = arcpy.mapping.MapDocument(mapDoc). Which means it doesn't like my mxd.
0 Kudos
by Anonymous User
Not applicable
i am getting a exceptions.KeyError'>:u'Amarillo_Base_Map'. Which is wierd where is it getting the u?
The error means this : exception KeyError
Raised when a mapping (dictionary) key is not found in the set of existing keys.

Could it be that there are 22 pdfs and the numbers in the program only go up to 9. So should I make them infinity because Houston has 154 pages.


The u' is just for unicoding...What numbers are you talking about that only go up to 9? Do you mean from the strip('1234567890')?  That will shave off any number from the string, no matter how high the number.  Would it be possible for you to send me a sample of your data? like maybe the 2 of the grid shapefiles that have the Sheet_ID field and if possible the corresponding pdf's?
0 Kudos
MichelleCouden1
Deactivated User
Found it!!!!   IT WORKS BEAUTIFULLY!!! THANK YOU, THANK YOU, THANK YOU FOR YOUR HELP!!!
It was how I was having it find the layer with the name (grid) in it. I went ahead and deleted all that and just used mxd like you and let the program find the Sheet_ID field all by itself!!!

WOW!! I can offically say I am learning python quickly!!!

Have a wonderful weekend!!!!!!!
0 Kudos
MichelleCouden1
Deactivated User
One more thing where can I look in the code to make sure it is counting the sheets right. I'm getting a shortage of pages. I'll send you the completed window. Let me know if you still need my files.
0 Kudos