Creating a perl script to batch process files

会有一股神秘感。 提交于 2020-01-06 03:40:22

问题


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


回答1:


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



回答2:


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



回答3:


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


来源:https://stackoverflow.com/questions/5347553/creating-a-perl-script-to-batch-process-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!