1

I'm using a script that extracts text from a PDF file. If I run my script one line at a time in the shell, it works fine (i.e. the extracted text is returned in the shell window), but if I try and execute the entire script, nothing gets returned. The script is as follows:

import PyPDF2
pdfFileObj = open('c:\Python27\meetingminutes.pdf', 'rb') 
pdfReader = PyPDF2.PdfFileReader(pdfFileObj) 
pdfReader.numPages 
pageObj = pdfReader.getPage(0) 
pageObj.extractText()

I'm very new to Python, so all help is appreciated!

Martijn Pieters
1.1m326 gold badges4.2k silver badges3.5k bronze badges
asked Oct 1, 2015 at 15:34
2
  • 3
    print(pageObj.extractText()) Commented Oct 1, 2015 at 15:35
  • "I'm very new to Python" - In that case, may I suggest that you stop what you are doing and read the Python Tutorial? It will answer this and many other questions even before you ask them. Commented Oct 1, 2015 at 15:39

1 Answer 1

5

The Python shell echoes the results of expressions. In a script, you need to explicitly print your results:

print pageObj.extractText()

If Python where to behave differently, you could never write a script that remained silent.

Technically speaking, what the Python interactive shell does is use the repr() function, so every expression (unless it produces None) is written using print repr(<expression outcome>). print without repr() would use the str() function instead.

answered Oct 1, 2015 at 15:35
Sign up to request clarification or add additional context in comments.

1 Comment

@RolfofSaxony: typo / thinko. The interactive interpreter is also called a REPL, a Read, Evaluate, Print Loop, which is probably why I slipped up.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.