
私は、OCR を使用して PDF ファイルを TXT 形式に変換する、うまく機能するスクリプトを見つけました。
しかし、毎回 1 つの 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
上記で示したスクリプトはどこにありますか。
また、一時的に作成されたファイルをクリーンアップする必要があることにも注意してください。