2017-05-31 130 views
-1

我試圖將所有pdf連接成一個pdf,從而使用PyPDF2庫。 我使用相同的Python 2.7。IOError:[Errno 22]無效的參數

我的錯誤是:

>>> 
RESTART: C:\Users\Yash gupta\Desktop\first projectt\concatenate\test\New folder\test.py 
['Invoice.pdf', 'Invoice_2.pdf', 'invoice_3.pdf', 'last.pdf'] 

Traceback (most recent call last): 
    File "C:\Users\Yash gupta\Desktop\first projectt\concatenate\test\New folder\test.py", line 17, in <module> 
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
    File "C:\Python27\lib\site-packages\PyPDF2\pdf.py", line 1084, in __init__ 
    self.read(stream) 
    File "C:\Python27\lib\site-packages\PyPDF2\pdf.py", line 1689, in read 
    stream.seek(-1, 2) 
IOError: [Errno 22] Invalid argument 

我的代碼是:

import PyPDF2, os 
# Get all the PDF filenames. 
pdfFiles = [] 
for filename in os.listdir('.'): 
    if filename.endswith('.pdf'): 
     pdfFiles.append(filename) 
pdfFiles.sort(key=str.lower) 
pdfWriter = PyPDF2.PdfFileWriter() 

print (pdfFiles) 

# Loop through all the PDF files. 
for filename in pdfFiles: 
    pdfFileObj = open(filename, 'rb') 
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
print (pdfFileObj) 

# Loop through all the pages 
for pageNum in range(0, pdfReader.numPages): 
    pageObj = pdfReader.getPage(pageNum) 
    pdfWriter.addPage(pageObj) 

# Save the resulting PDF to a file. 
pdfOutput = open('last.pdf', 'wb') 
pdfWriter.write(pdfOutput) 
pdfOutput.close() 

我的PDF有一些非ASCII字符,所以我用 'R' Rathen市的那麼 'RB'

PS:我是新來的Python和所有這些庫的東西

回答

1

我相信你正在循環收集的文件不正確y(Python是縮進敏感的)。

# Loop through all the PDF files. 
for filename in pdfFiles: 
    pdfFileObj = open(filename, 'rb') 
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 

    # Loop through all the pages 
    for pageNum in range(0, pdfReader.numPages): 
     pageObj = pdfReader.getPage(pageNum) 
     pdfWriter.addPage(pageObj) 

    # Save the resulting PDF to a file. 
    pdfOutput = open('last.pdf', 'wb') 
    pdfWriter.write(pdfOutput) 
    pdfOutput.close() 

另外,儘量使用PdfFileMerger如果要合併的PDF文件:

merger = PdfFileMerger(strict=False) 

Check out the example code here

+0

謝謝,這樣做,但現在這個錯誤就要 文件 「C:\ Python27 \ LIB \站點包\ PyPDF2 \ generic.py」,線路585,在readFromStream %(utils.hexStr(stream.tell ()),key)) PdfReadError:字典中字節0x2695處多個定義的鍵/類型 你能幫忙嗎? –

+0

@YashGupta更新了我的答案。 – errata

相關問題