user3848207
user3848207

Reputation: 4907

Cannot merge pdf in python v3.6

I have the following code segment which has been tested to work in python ver2.7 The code merges multiple pdfs into a single pdf.

from PyPDF2 import PdfFileMerger, PdfFileReader

#merge individual pdfs of each page into a single pdf
merger = PdfFileMerger()
for filename in pdf_list:
    merger.append(PdfFileReader(file("./" + pdf_location + "/" + filename, 'rb')))

When I run the same code in python v3.6, it fails and the following error is printed.

NameError: name 'file' is not defined

How should the code be modified to get it working in python v3.6?

Upvotes: 0

Views: 243

Answers (1)

Partha Das
Partha Das

Reputation: 180

I haven't used PdfFileReader before, but from the documentation, it requires a filestream as an argument. So try changing "file" to "open" which should pass on a filestream pointed to the location in the read binary mode to the PdfFileReader constructor. So your append line should read as follows:

merger.append(PdfFileReader(open("./" + pdf_location + "/" + filename, 'rb')))

Upvotes: 1

Related Questions