开发者

Creating a perl script to batch process files

开发者 https://www.devze.com 2023-02-17 18:59 出处:网络
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 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 numb开发者_JAVA百科er 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


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");
}


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


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? ')))]"
0

精彩评论

暂无评论...
验证码 换一张
取 消