3
PyPDFモジュールを使用してPythonスクリプトを作成しようとしています。スクリプトは 'Root'フォルダを取ってその中のすべてのPDFをマージし、 'Output'フォルダにマージしたPDFを出力し、 'Root.pdf'(分割されたPDFを含むフォルダ)に名前を変更します。それはサブディレクトリと同じことを行い、最終出力にサブディレクトリと同じ名前を与えます。Pythonスクリプト(pypdf/16進数エラー)の実行に関する問題
サブディレクトリを処理しようとしていて、いくつかの16進値に関連するエラーコードが表示されます。
Traceback (most recent call last):
File "C:\Documents and Settings\student3\Desktop\Test\pdfMergerV1.py", line 76, in <module>
files_recursively(path)
File "C:\Documents and Settings\student3\Desktop\Test\pdfMergerV1.py", line 74, in files_recursively
os.path.walk(path, process_file,())
File "C:\Python27\lib\ntpath.py", line 263, in walk
walk(name, func, arg)
File "C:\Python27\lib\ntpath.py", line 259, in walk
func(arg, top, names)
File "C:\Documents and Settings\student3\Desktop\Test\pdfMergerV1.py", line 38, in process_file
pdf = PdfFileReader(file(filename, "rb"))
File "C:\Python27\lib\site-packages\pyPdf\pdf.py", line 374, in __init__
self.read(stream)
File "C:\Python27\lib\site-packages\pyPdf\pdf.py", line 775, in read
newTrailer = readObject(stream, self)
File "C:\Python27\lib\site-packages\pyPdf\generic.py", line 67, in readObject
return DictionaryObject.readFromStream(stream, pdf)
File "C:\Python27\lib\site-packages\pyPdf\generic.py", line 531, in readFromStream
value = readObject(stream, pdf)
File "C:\Python27\lib\site-packages\pyPdf\generic.py", line 58, in readObject
return ArrayObject.readFromStream(stream, pdf)
File "C:\Python27\lib\site-packages\pyPdf\generic.py", line 153, in readFromStream
arr.append(readObject(stream, pdf))
File "C:\Python27\lib\site-packages\pyPdf\generic.py", line 69, in readObject
return readHexStringFromStream(stream)
File "C:\Python27\lib\site-packages\pyPdf\generic.py", line 276, in readHexStringFromStream
txt += chr(int(x, base=16))
ValueError: invalid literal for int() with base 16: '\x00\x00'
これは、スクリプトのソースコードです:
エラーコード生成がある。ここ(六角形ではないnull値を取得しているようです)
#---------------------------------------------------------------------------------------------- # 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 <strong class="highlight">python</strong> import os import glob import sys import fnmatch from pyPdf import PdfFileReader, PdfFileWriter output = PdfFileWriter() path = str(os.getcwd()) x = 0 def process_file(_, path, filelist): for filename in filelist: if filename.endswith('.pdf'): filename = os.path.join(path, filename) print "Merging " + filename pdf = PdfFileReader(file(filename, "rb")) x = pdf.getNumPages() i = 0 while (i != x): output.addPage(pdf.getPage(i)) print "Merging page: " + str(i+1) + "/" + str(x) i += 1 output_dir = "\Output\\" ext = ".pdf" dir = os.path.basename(path) outputpath = str(os.getcwd()) + output_dir final_output = outputpath if os.path.exists(final_output) != True: os.mkdir(final_output) outputStream = file(final_output + dir + ext, "wb") os.path.join(outputStream) output.write(outputStream) outputStream.close() else: outputStream = file(final_output + dir + ext, "wb") os.path.join(outputStream) output.write(outputStream) outputStream.close() def files_recursively(topdir): os.path.walk(path, process_file,()) files_recursively(path)
PDFはすべて正常であるようです。どのファイルリーダーでも開くことができます。 output_dirに関しては、Pythonはそれ以外の方法で私にそれをさせません。 – Brian
PDFはすべて正常であるようです。どのファイルリーダーでも開くことができます。 output_dirに関しては、Pythonは私にそうしないようにしません。 – Brian
私が考えることができるのは、PyPDFがこれらのファイルを読むのに十分に豊富ではないということです。あなたのoutput_dirについては、 "Output \\"に変更してください。問題は解決しませんが、少なくとも余分なバックスラッシュ –