0

I am losing my mind.

This code is giving me a syntax error complaining about the second if statement. Shouldn't everything be working? I fail to see the mistake, I tried adding an else statement but that just makes that the syntax error..

Help highly appreciated!

inputpdf = PyPDF2.PdfFileReader("testfolder\\merge.pdf")

for i in range(inputpdf.numPages):
    num = range(inputpdf.numPages)[-1]
    if (num % 2) != 0:
        print(i)
        output = PyPDF2.PdfFileWriter()
        output.addPage(inputpfd.getPage(range(num))
    if (i % 2) == 0:
        output = PyPDF2.PdfFileWriter()
        output.addPage(inputpdf.getPage(i))
        output.addPage(inputpdf.getPage(i+1))
        with open("document-page%s.pdf" % i, "wb") as outputStream:
            output.write(outputStream)
1
  • FWIW: Python 3.10 will issue a much more useful warning in this case (SyntaxError: '(' was never closed), which should make it easier to catch typos like this in the future. Commented Jan 27, 2022 at 8:11

2 Answers 2

2

Replace

output.addPage(inputpfd.getPage(range(num)) //no enclosing brace.

with

output.addPage(inputpfd.getPage(range(num))) 
Sign up to request clarification or add additional context in comments.

Comments

2

you missed one enclosing brace ):

import PyPDF2

inputpdf = PyPDF2.PdfFileReader("testfolder\\merge.pdf")

for i in range(inputpdf.numPages):
    num = range(inputpdf.numPages)[-1]
    if (num % 2) != 0:
        print(i)
        output = PyPDF2.PdfFileWriter()
        output.addPage(inputpdf.getPage(range(num)))
    if (i % 2) == 0:
        output = PyPDF2.PdfFileWriter()
        output.addPage(inputpdf.getPage(i))
        output.addPage(inputpdf.getPage(i+1))
        with open("document-page%s.pdf" % i, "wb") as outputStream:
            output.write(outputStream)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.