2011-08-05 3 views
3

PyPDF 모듈을 사용하여 Python 스크립트를 만들려고합니다. 스크립트는 'Root'폴더를 사용하여 모든 PDF를 병합하고 병합 된 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) 

답변

0

당신이 읽는 PDF 파일이 유효한 PDF 파일이 아니거나 PyPDF가 준비된 것보다 더 이국적인 것 같습니다. 읽을 수있는 좋은 PDF 파일이 있습니까?

또한, 코드에서 몇 가지 이상한 일이있다, 그러나 이것은 정말 문제 수도 :
output_dir = "\Output\\" 

당신은 당신이 원하는하지 않은 거기 \O 이스케이프 시퀀스가 ​​있습니다.

+0

PDF는 모두 정상적인 것 같습니다. 모든 파일 판독기에서 열 수 있습니다. output_dir에 관해서는, 파이썬은 그렇지 않다면 그렇게하지 않을 것입니다. – Brian

+0

PDF는 모두 정상적으로 보이지만 모든 파일 판독기에서 열 수 있습니다. output_dir에 관해서는, 파이썬은 그렇지 않으면 내가하지 못하게 할 것이다. – Brian

+0

필자가 생각할 수있는 것은 PyPDF가 이러한 파일을 읽을만큼 충분히 풍부하지 않다는 것입니다. output_dir의 경우 "Output \\"로 변경하십시오. 문제가 해결되지는 않지만 최소한 여분의 백 슬래시는 제거됩니다. –