Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have the front and back side scans of about 1000 separate documents in 2 sepatate folders. I'm looking to create a batch operation that combines the front scan and its corresponding back side scan into one document.

EDIT: I'm using Windows XP, The Scans are PDFs. The front covers are in one folder and the back covers are in another. The Files are named 1-1-NAME, 1-2-NAME; where NAME is a four letter idenifier.

share|improve this question
1  
Please give some more informations: What OS are you using? What kind of documents (JPG, PDF)? Are the front covers in one folder and the back covers in the other? How are these files named? –  mpy Apr 30 '13 at 14:41
    
Closely related: How to merge multiple PDF files onto one page with pdftk? –  slhck Apr 30 '13 at 21:47

1 Answer 1

Are you looking for ImageMagick's montage? ImageMagick can work on pdf.

If you want something more flexible than ImageMagick montage, you can also write a simple script in Python language with pyPdf library. pyPdf can merge PDF pages and apply basic transformations (e.g. translation, rotation, scaling). Example script:

import pyPdf

def merge_horizontal(out_filename, left_filename, right_filename):
    """ Merge the first page of two PDFs side-to-side """

    # open the PDF files to be merged
    with open(left_filename) as left_file, open(right_filename) as right_file, open(out_filename, 'w') as output_file:
        left_pdf = pyPdf.PdfFileReader(left_file)
        right_pdf = pyPdf.PdfFileReader(right_file)
        output = pyPdf.PdfFileWriter()

        # get the first page from each pdf
        left_page = left_pdf.pages[0]
        right_page = right_pdf.pages[0]

        # start a new blank page with a size that can fit the merged pages side by side
        page = output.addBlankPage(
            width=left_page.mediaBox.getWidth() + right_page.mediaBox.getWidth(),
            height=max(left_page.mediaBox.getHeight(), right_page.mediaBox.getHeight()),
        )

        # draw the pages on that new page
        page.mergeTranslatedPage(left_page, 0, 0)
        page.mergeTranslatedPage(right_page, left_page.mediaBox.getWidth(), 0)

        # write to file
        output.write(output_file)


def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if not (exc.errno == errno.EEXIST and os.path.isdir(path)): 
            raise
if __name__ == '__main__':
    import sys, os, errno
    output_folder_name = sys.argv[1]
    left_folder_name = sys.argv[2]
    right_folder_name = sys.argv[3]
    left_files = set(os.listdir(left_folder_name))
    right_files = set(os.listdir(right_folder_name))
    mkdir_p(output_folder_name)

    # for every files that are in both left_files and right_files
    for f in left_files.intersection(right_files):
        output_file_name = os.path.join(output_folder_name, f)
        left_file_name = os.path.join(left_folder_name, f)
        right_file_name = os.path.join(right_folder_name, f)
        print 'merging %s and %s into %s' % (left_file_name, right_file_name, output_file_name)
        merge_horizontal(output_file_name, left_file_name, right_file_name)

    # pair is missing, not merging
    print 'Only in left folder: ', left_files - right_files
    print 'Only in right folder: ', right_files - left_files

And call the script like the following:

python merge.py output_folder left_folder right_folder

Sample output:

merging folderA/two.pdf and folderB/two.pdf into output/dacd/adca/two.pdf
merging folderA/one.pdf and folderB/one.pdf into output/dacd/adca/one.pdf
merging folderA/three.pdf and folderB/three.pdf into output/dacd/adca/three.pdf
Only in left folder:  set(['four.pdf'])
Only in right folder:  set(['five.pdf'])
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.