1

ファイル名を含む配列を持つスクリプトを作成しました。スクリプトは、再帰によってディレクトリとサブディレクトリを介して pdf ファイルを検索し、それらを配列に追加します。次に、pdftk のコマンド ラインに文字列を出力して、それらをマージします。

pdftk は次のような引数を取ります:

pdftk inputpdf1.pdf inputpdf2.pdf cat output output.pdf

ただし、入力されたパスは、Windows コマンド (上記) から表示されるエラー メッセージによると正しくないようです。Ubuntu でも同じエラーが発生します。

    Microsoft Windows XP [Version 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

C:\Documents and Settings\student3>cd C:\Documents and Settings\student3\Desktop
\Test

C:\Documents and Settings\student3\Desktop\Test>pdftest.py
Merging C:\Documents and Settings\student3\Desktop\Test\1.pdf
pdftk "C:\Documents and Settings\student3\Desktop\Test\1.pdf" cat outputC:\Docum
ents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
pdftk "C:\Documents and Settings\student3\Desktop\Test\1.pdf" cat outputC:\Docum
ents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Merging C:\Documents and Settings\student3\Desktop\Test\2.pdf
pdftk "C:\Documents and Settings\student3\Desktop\Test\2.pdf" cat outputC:\Docum
ents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
pdftk "C:\Documents and Settings\student3\Desktop\Test\2.pdf" cat outputC:\Docum
ents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Merging C:\Documents and Settings\student3\Desktop\Test\brian\1.pdf
pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\1.pdf" cat outputC:
\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\1.pdf" cat outputC:
\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Merging C:\Documents and Settings\student3\Desktop\Test\brian\2.pdf
pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\2.pdf" cat outputC:
\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
pdftk "C:\Documents and Settings\student3\Desktop\Test\brian\2.pdf" cat outputC:
\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Merging C:\Documents and Settings\student3\Desktop\Test\testing\1.pdf
pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\1.pdf" cat output
C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\1.pdf" cat output
C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Merging C:\Documents and Settings\student3\Desktop\Test\testing\2.pdf
pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\2.pdf" cat output
C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
pdftk "C:\Documents and Settings\student3\Desktop\Test\testing\2.pdf" cat output
C:\Documents and Settings\student3\Desktop\Test\Output\.pdf
Error: Unexpected text in page reference, here:
   outputC:\Documents
   Exiting.
   Acceptable keywords, here, are: "even", "odd", or "end".
Errors encountered.  No output created.
Done.  Input errors, so no output created.
Finished Processing

C:\Documents and Settings\student3\Desktop\Test>

スクリプトのコードは次のとおりです。

#----------------------------------------------------------------------------------------------
# Name:        pdfMerger
# Purpose:     Automatic merging of all PDF files in a directory and its sub-directories and
#              rename them according to the folder itself. Requires the pyPDF Module
#
# Current:     Processes all the PDF files in the current directory
# To-Do:       Process the sub-directories.
#
# Version: 1.0
# Author:      Brian Livori
#
# Created:     03/08/2011
# Copyright:   (c) Brian Livori 2011
# Licence:     Open-Source
#---------------------------------------------------------------------------------------------
#!/usr/bin/env python

import os
import glob
import sys
import fnmatch
import subprocess

path = str(os.getcwd())


x = 0

def process_file(_, path, filelist):
    os.path.walk(os.path.realpath(topdir), process_file, ())
    input_param = " ".join('"' + x + '"' for x in glob.glob(os.path.join(path, "*.pdf"))

    output_param = '"' + os.path.join(path, os.path.basename(path) + ".pdf") + '"'

    cmd = "pdftk " + input_param + " cat output " + output_param
    os.system(cmd)


    for filenames in os.walk (path):
         if "Output" in filenames:
            filenames.remove ("Output")

    if os.path.exists(final_output) != True:

                    os.mkdir(final_output)
                    sp = subprocess.Popen(cmd)
                    sp.wait()


    else:

                   sp = subprocess.Popen(cmd)
                   sp.wait()




def files_recursively(topdir):
 os.path.walk(os.path.realpath(topdir), process_file, ())

files_recursively(path)

print "Finished Processing"

私は正確に何を間違っていますか?

File "C:\Documents and Settings\student3\Desktop\Test\pdftest2.py", line 32
    output_param = '"' + os.path.join(path, os.path.basename(path) + ".pdf") + '"'
               ^
SyntaxError: invalid syntax
4

2 に答える 2

3

空白があるため、パスを二重引用符で囲んでエスケープする必要があります。それ以外の場合、シェルはすべての空白を新しいファイルの区切り文字として解釈します。

" ".join('"' + str(f) + '"' for f in filesArr)

さらにいくつかのこと:

  1. PDFごとにPDFTKを呼び出します。それをループから外して、ファイルの入力リストを作成する必要があります。(すべての入力PDFを1つの出力PDFにマージしたいとします
  2. 猫の出力後にスペースがありません

    ... " cat output " + outputpath + ext)

  3. 変数outputpathは空です。

編集:

あなたのコードは少し混乱しています。process_fileメソッドを次のように変更します。

def process_file(_, path, filelist):
    input_param = " ".join('"' + x + '"' for x in glob.glob(os.path.join(path, "*.pdf"))
    output_param = '"C:\ENTER\OUTPUT\PATH\HERE.PDF"'
    cmd = "pdftk " + input_param + " cat output " + output_param
    os.system(cmd)

なぜそこにすべての割り当てが必要なのか、私にはよくわかりません。

編集2:

ここに私の完全なスクリプトがあります:

#!/usr/bin/env python

import os
import glob

def process_file(_, path, filelist):
    input_param = " ".join('"' + x + '"' for x in glob.glob(os.path.join(path, "*.pdf"))))
    output_param = '"' + os.path.join(path, os.path.basename(path) + ".pdf") + '"'
    cmd = "pdftk " + input_param + " cat output " + output_param
    print cmd
    os.system(cmd)

def files_recursively(topdir):
    os.path.walk(os.path.realpath(topdir), process_file, ())

if  __name__ == "__main__":
    files_recursively(os.getcwd())

そしてここPastebinで

それが生成するコマンド:

pdftk "/home/user/pdf/Test1.pdf" "/home/user/pdf/Test3.pdf" "/home/user/pdf/Test2.pdf" cat output "/home/user/pdf/pdf.pdf"
pdftk "/home/user/pdf/Sub3/Test1.pdf" "/home/user/pdf/Sub3/Test3.pdf" "/home/user/pdf/Sub3/Test2.pdf" cat output "/home/user/pdf/Sub3/Sub3.pdf"
pdftk "/home/user/pdf/Sub2/Test1.pdf" "/home/user/pdf/Sub2/Test3.pdf" "/home/user/pdf/Sub2/Test2.pdf" cat output "/home/user/pdf/Sub2/Sub2.pdf"
pdftk "/home/user/pdf/Sub2/SubSub21/Test1.pdf" "/home/user/pdf/Sub2/SubSub21/Test3.pdf" "/home/user/pdf/Sub2/SubSub21/Test2.pdf" cat output "/home/user/pdf/Sub2/SubSub21/SubSub21.pdf"
pdftk "/home/user/pdf/Sub2/SubSub22/Test1.pdf" "/home/user/pdf/Sub2/SubSub22/Test3.pdf" "/home/user/pdf/Sub2/SubSub22/Test2.pdf" cat output "/home/user/pdf/Sub2/SubSub22/SubSub22.pdf"
pdftk "/home/user/pdf/Sub1/Test1.pdf" "/home/user/pdf/Sub1/Test3.pdf" "/home/user/pdf/Sub1/Test2.pdf" cat output "/home/user/pdf/Sub1/Sub1.pdf"
pdftk "/home/user/pdf/Sub1/SubSub2/Test1.pdf" "/home/user/pdf/Sub1/SubSub2/Test3.pdf" "/home/user/pdf/Sub1/SubSub2/Test2.pdf" cat output "/home/user/pdf/Sub1/SubSub2/SubSub2.pdf"
pdftk "/home/user/pdf/Sub1/SubSub1/Test1.pdf" "/home/user/pdf/Sub1/SubSub1/Test3.pdf" "/home/user/pdf/Sub1/SubSub1/Test2.pdf" cat output "/home/user/pdf/Sub1/SubSub1/SubSub1.pdf"
于 2011-08-10T11:56:02.140 に答える
0

コマンドと引数os.system()subprocess.Popenリストとして指定すると、このモジュールの内容はファイル名のスペースを適切に処理します。

Windows の場合: Popen クラスは CreateProcess() を使用して、文字列を操作する子プログラムを実行します。args がシーケンスの場合、list2cmdline メソッドを使用して文字列に変換されます。すべての MS Windows アプリケーションがコマンド ラインを同じように解釈するわけではないことに注意してください。list2cmdline は、MS C ランタイムと同じ規則を使用するアプリケーション用に設計されています。

あなたの例では、それは

cmd = ["pdftk"] + files_arr + "cat", "output", outputpath + ext]

その後

sp = subprocess.Popen(cmd)
sp.wait()
于 2011-08-10T12:14:15.580 に答える