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!
1 Answer 1
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.
print(pageObj.extractText())