want to check for blank pages in a pdf with Acrobat X

Asked by rob

Hello,
im trying to write a short script that will delete blank pages from a pdf using adobe acrobat X standard. it seems to correctly identify blank pages. but for some reason if a blank page is found it deletes every page in the pdf. if i run the script on a 1 page pdf that is not blank it works. the non blank pages have black text on white or blue backgrounds and there are text boxes with red outlines so they are very different from blank pages.
here is the script

from org.sikuli.script.natives import Vision
Vision.setParameter("MinTargetSize",12)
mmd = Settings.MoveMouseDelay
Settings.MoveMouseDelay = 0.01

def deleteBlankPages(numPages):
    '''
    identifies and deletes blank pages in a pdf
    numPages - integer. the number of pages in the pdf
    '''
    adobe = App('Adobe Acrobat')
    adobe.focus()
    workReg = find("1370615503919.png").above(1).below() # the toolbar area
# workReg.highlight(3)
    searchReg = find("1370620692978.png").below() # the pdf viewing area
    searchReg.highlight(3)
    for i in range(numPages):
        try:
            searchReg.find(Pattern("1370620830920.png").similar(0.90)) # find a blank page pattern
            type('D', KeyModifier.SHIFT + KeyModifier.CTRL) # and delete it
            wait(0.5)
            type(Key.ENTER)
            wait(0.2)
            type(Key.ENTER)
        except FindFailed: # if blank page not found, go to next page
            type(Key.RIGHT)
if __name__ == '__main__':
    deleteBlankPages(8)

thanks in advance for you help

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
rob
Solved:
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

this would be my approach:

   for i in range(numPages):
        if searchReg.exists(Pattern("1370620830920.png").similar(0.99), 0):
            # found a blank page pattern
            type('D', KeyModifier.SHIFT + KeyModifier.CTRL) # and delete it
            wait(0.5)
            type(Key.ENTER)
            wait(0.2)
            type(Key.ENTER)
        else: # if blank page not found, go to next page
            type(Key.RIGHT)
            wait(1)

- take care that the shot for Pattern("1370620830920.png") is good enough to always produce a similarity of 1.0 for a blank page
- I use exists(image, 0), to immediately return after the first search and to be able to use if/else
- added a wait when switching to next page, to assure that the switching has finished properly before the next search

Revision history for this message
RaiMan (raimund-hocke) said :
#2

just realized:
a wait(1) might be as well needed after the last type(Key.ENTER)

Revision history for this message
rob (reg82) said :
#3

Thanks RaiMan. it works like a charm. i have been manually deleting randomly inserted blank pages from 1000+ pg documents and this is going to save me so much time and clicking. i love this software and this forum.