複数のテキストファイルをpdfに変換し、ヘッダファイルに基づいて名前を付けます。

複数のテキストファイルをpdfに変換し、ヘッダファイルに基づいて名前を付けます。

「テキストを.pdfに変換する方法」への答えはすでにここにあります。協会そしてここ協会、私はより具体的なものを探しています。

Claws-Mailの使用[ウェブサイト]とプラグイン[RSSyl] RSSフィードを読むために大量のテキストファイルを収集しました。これを.pdfファイルに変換したいです。

質問:フォルダのファイル番号は[1、2、… 、456] です。各フィードには独自のフォルダがありますが、内部には「ちょうど」番号付きのファイルしかありません。各ファイルにはヘッダー(後にメッセージの内容が続く)が含まれています。

Date: Tue,  5 Feb 2013 19:59:53 GMT
From: N/A
Subject: Civilized Discourse Construction Kit
X-RSSyl-URL: http://www.codinghorror.com/blog/2013/02/civilized-discourse-construction-kit.html
Message-ID: <http://www.codinghorror.com/blog/2013/02/civilized-discourse-construction-kit.html>
Content-Type: text/html; charset=UTF-8

<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<base href="http://www.codinghorror.com/blog/2013/02/civilized-discourse-construction-kit.html">
</head><body>
<p>URL: <a href="http://www.codinghorror.com/blog/2013/02/civilized-discourse-construction-kit.html">http://www.codinghorror.com/blog/2013/02/civilized-discourse-construction-kit.html</a></p>
<br>
<!-- RSSyl text start -->

質問.pdf:各ファイルをファイルに変換し、以下の名前に合わせて名前を変更する方法トピック。本当にクールなのは、次のように変換して名前を変更することです。

"folder.name"_"date"_"file name"各情報はヘッダーデータから取得されます。何百ものファイルがあり、一括処理できる方法を探しています。

ファイルはhtmlフォーマットされていますが、サフィックスはありません.htm[l]

ベストアンサー1

1つのレベルのディレクトリしかない比較的単純なファイルツリーがあり、各ディレクトリにファイルのリストが含まれているがサブディレクトリがない場合は、次のことができます(ターミナルに直接貼り付けてをクリックできますEnter)。

for dir in *; do    ## For each directory
 if [ "$(ls -A "$dir")" ]; then  ## If the dir is not empty
   for file in "$dir"/*; do      ## For each file in $dir
    i=0;                         ## initialize a counter
    ## Get the subject
    sub=$(grep ^Subject: "$file" | cut -d ':' -f 2-);
    ## get the date, and format it to MMDDYY_Hour:Min:Sec
    date=$(date -d "$(grep ^Date: $file | cut -d ':' -f 2-)" +%m%d%y_%H:%M:%S);
    ## the pdf's name will be <directory's name> _ <date> _ <subject>
    name="$dir"_"$date"_"$sub";
    ## if a file of this name exists
    while [ -e "$dir/$name".pdf ]; do
      let i++;                       ## increment the counter
      name="$dir"_"$date"_"$sub"$i;  ## append it to the pdf's name
    done;
    wkhtmltopdf "$file" "$dir"/"$name".pdf; ## convert html to pdf
  done
 fi
done

ノート

  • このソリューションには次のものが必要です。wkhtmltopdf:

    Webkitレンダリングエンジンとqtを使用してhtmlをpdfに変換するシンプルなシェルユーティリティです。

    Debian ベースのシステムでは、次のコマンドを使用してインストールできます。

    sudo apt-get install wkhtmltopdf
    
  • そこにいるとします。ファイルなしトップレベルディレクトリに必要なhtmlファイルのみが必要です。すべてのサブディレクトリにあります。

  • スペース、改行、その他の不正文字を含むファイルとディレクトリ名を処理できます。

  • dir1/fooあなたが投稿したサンプルコンテンツを含むファイルが与えられると、次のファイルが生成されます。dir1/dir1_020513_20:59:53_Civilized Discourse Construction Kit10.pdf

おすすめ記事