一個讓另一個腳本按順序處理所有文件的腳本?

一個讓另一個腳本按順序處理所有文件的腳本?

我找到了一個很好用的腳本,可以使用 ocr 將 pdf 檔案轉換為 txt 格式。

但它每次只轉換一個pdf檔。我需要對它們進行大規模轉換。

我對劇本創作一竅不通。腳本如下。

我怎樣才能批量轉換它們?

#!/bin/bash

## script to:
##   *  split a PDF up by pages
##   *  convert them to an image format
##   *  read the text from each page
##   *  concatenate the pages


## pass name of PDF file to script
INFILE=$1

## split PDF file into pages, resulting files will be
## numbered: pg_0001.pdf  pg_0002.pdf  pg_0003.pdf
pdftk $INFILE burst

for i in pg*.pdf ; do

    ## convert it to a PNG image file
    convert -density 200 -quality 100 $i ${i%.pdf}.png

    ## read text from each page
    tesseract ${i%.pdf}.png ${i%.pdf}.txt

done

## concatenate the pages into a single text file
cat pg*.txt > ${INFILE%.pdf}.txt

exit

注意:我讀過類似的問題,但無法弄清楚。

答案1

您可以修改您的腳本:

# instead of INFILE=$1
for INFILE
do
#...

    for i in pg*.pdf ; do
        #...    
    done

    ## concatenate the pages into a single text file
    cat pg*.txt > ${INFILE%.pdf}.txt
done

然後這樣呼叫你的腳本:

some-script.sh 1.pdf 2.pdf #...

當沒有給定任何要循環的內容時,循環bash for將循環遍歷所有命令列參數。因此,

for INFILE

相當於:

for INFILE in "$@"

答案2

根據我對你的問題的理解,我想這就是你所期望的:

for each in *.pdf
do
  your_conv_script.sh $each
done

your_conv_script.sh您上面指出的腳本在哪裡。

另請注意,您需要清理臨時建立的檔案。

相關內容