سؤال

Is there a working PDF manipulation module for Python 3? I've tried Pypdf, but it glitches out when I try to install with PIP. I'd like to merge PDF files. If I use Pypdf2, I get the following message using this code:

from pypdf2 import merger, PdfFileReader    

with open('test1.pdf', 'rb') as f:
        with open('test2.pdf', 'rb') as f2:
            merger = PdfFileMerger()
            merger.merge(position=0, fileobj=f2)
            merger.merge(position=0, fileobj=f)
            merger.write(open("test_out.pdf", 'wb'))
"File "c:\...merger.py", line 97, in merge
elif type(fileobj) == file:
NameError: global name 'file' is not defined"

Line 97 of merger.py is:

elif type(fileobj) == file:

I get similar errors in my own code when using code such as

input1 = PdfFileReader(file("document1.pdf", "rb")) - that's a copy and paste from http://www.blog.pythonlibrary.org/2012/07/11/pypdf2-the-new-fork-of-pypdf/

هل كانت مفيدة؟

المحلول

It seems that is a bug in PyPDF2... file is gone in python3, that's why you get an error here.

A quick fix would be to add this to the imports in merger.py:

from io import FileIO as file
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top