1

I have this line of code:

convert 1234_Page_1_....png 1234_Page_2_....png output.pdf 

This merges those particular pngs to a single pdf (using ImageMagick). I have a bunch of files in this format. I would like to perform this merging/converting-to-pdf action on files that have the same number before the "Page". Sometimes there are more than two pages to convert.

I would like to have this done in a perl script that I can run on Windows.

Thanks in advance, Jake

3 Answers 3

2

If you wanted to call convert(1) as few times as necessary:

#! /usr/bin/perl

use strict;
use warnings;

my %processed = ();
for my $prefix (map { /^(\d+)/ } glob('[1-9]*_Page_*.png')) {
    next if $processed{$prefix}++;
    system("convert ${prefix}_Page_*.png ${prefix}_output.pdf");
}
1

If you have cygwin (maybe mingw too?) installed, try this:

for i in `seq 1234 1350` ; do convert ${i}_Page_*.png ${i}_output.pdf ; done
6
  • Thanks, but if there is a file missing from the sequence, will the script hang up?
    – Jake
    Mar 18, 2011 at 3:13
  • Jake, no, but it will throw ugly error messages that might hide important failures.
    – sarnold
    Mar 18, 2011 at 3:17
  • I got this message: Missing $ on loop variable at script.pl line 1.
    – Jake
    Mar 18, 2011 at 3:25
  • That's a bash-script, it won't work from perl. If you don't already have cygwin or mingw installed, it probably isn't worth the hassle. But if you do have them installed (and a lot of programmers do :) then it's an easy tool for some of these operations.
    – sarnold
    Mar 18, 2011 at 3:30
  • I got cygwin installed, but when I try running the script, i get the error: line:1 syntax error near unexpected token '$'\r'' line:1 'for i in 'seq 312800 312860' ;
    – Jake
    Mar 20, 2011 at 23:01
0

Can't you use an asterisk for this? I will try it right now.

convert 1234_Page_*.png output.pdf

If you want the readable file, here you go:

import os

for i in xrange(int(raw_input('How many sets of pages are there? '))):
  os.system('convert {0}_Page_*.png output_{0}.pdf'.format(str(i)))

Here's a one-liner:

python -c "import os; [os.system('convert {0}_Page_*.png output_{0}.pdf'.format(str(i))) for i in xrange(int(raw_input('How many sets of pages are there? ')))]"
8
  • I believe so, but I'm looking for help looping through all of the files that I have. I.e. 1234, 1235, 1236 etc. I have thousands of such files.
    – Jake
    Mar 18, 2011 at 3:06
  • Ohh, okay. Would you mind a Python script? I can make a single command to run for this really easily.
    – Blender
    Mar 18, 2011 at 3:09
  • Mine isn't much different than your's. Mine just asks you how many pages there are, and if you type in a big number, it goes into a death-count which locks up the console window until it finishes ;)
    – Blender
    Mar 18, 2011 at 3:23
  • Perhaps not that different, but Python is probably more ubiquitous than cygwin or mingw. :)
    – sarnold
    Mar 18, 2011 at 3:31
  • I actually am writing a Python shell, just because I can't figure out how to use Bash ;) It's called Pysh. Creative, huh?
    – Blender
    Mar 18, 2011 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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