IOError: [Errno 22] Invalid argument

IOError: [Errno 22] Invalid argument

我正在尝试将所有 pdf 连接成一个 pdf,从而使用 PyPDF2 库。 我正在使用 python 2.7。

我的错误是:

>>>
 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' 然后 'rb'

PS:我是 Python 和所有这些库的新手

我认为您错误地循环收集的文件(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()

此外,如果您想合并 PDF 文件,请尝试使用 PdfFileMerger

merger = PdfFileMerger(strict=False)

Check out the example code here.