我正在使用从 PDF 文件中提取文本的脚本。如果我在 shell 中一次一行地运行我的脚本,它工作正常(即提取的文本在 shell 窗口中返回),但如果我尝试执行整个脚本,则不会返回任何内容。脚本如下:
import PyPDF2
pdfFileObj = open('c:\Python27\meetingminutes.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pdfReader.numPages
pageObj = pdfReader.getPage(0)
pageObj.extractText()
我是 Python 的新手,所以非常感谢您的帮助!
请您参考如下方法:
Python shell 回显 表达式的结果。在脚本中,您需要显式打印您的结果:
print pageObj.extractText()
如果 Python 的行为方式不同,您将永远无法编写保持沉默的脚本。
从技术上讲,Python 交互式 shell 所做的是使用 repr() function , 所以每个表达式(除非它产生 None )都是使用 print repr(<expression outcome>) 编写的. print没有repr()会使用 str() function相反。
