Reputation: 25
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
Upvotes: 1
Views: 1416
Reputation: 58534
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");
}
Upvotes: 2
Reputation: 298156
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? ')))]"
Upvotes: 0