Select to view content in your preferred language

Comparing a Search Cursor list to number of PDFs Created.

6201
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, I think I got it from here. I did a process of elimination and made the program count. For example: Before, mapDoc in mxdList I put count = 0. Then before pdf in unique, I put count2 = 0. Then I put if count == count2 just before all the print messages. They both counted 11 sheets which is correct. So the problem is in the area where pdf_dict = {} and pdf_list = [] and so on. The code is below: I think I can fix it. But do me a favor and keep watch, because if I can't I'll put a new feed on the forum.

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

count = 0
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)
                count += 1
            sheet_count = max(max_list)
            mxd_name = p.basename(mapDoc).split('.')[0]
            mxd_dict[mxd_name] = sheet_count
                        
        except:
            pass
arcpy.AddMessage (count)

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))

count2 = 0
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)
                count2 += 1
            pdf_dict[doc_] = page_count

arcpy.AddMessage (count2)

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 count == count2:
        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)
        

0 Kudos
MichelleCouden1
Deactivated User
Answered!!
0 Kudos