python - 如何让这个Python方法返回一个字符串而不是将其写入stdout?

标签 python pdf return stdout pdfminer

我正在尝试使用 Python 从 pdf 中提取文本。为此我找到了pdfminer ,使用 pdf2txt.py command line tool 做得相当好。如下:

kramer65 $ pdf2txt.py myfile.pdf
all the text contents
of the pdf
are printed out here..

因为我想在我的程序中使用此功能,所以我想将其用作模块而不是命令行工具。所以我设法将 pdf2txt.py 文件调整为以下内容:

#!/usr/bin/env python
import sys
from pdfminer.pdfdocument import PDFDocument
from pdfminer.pdfparser import PDFParser
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.pdfpage import PDFPage
from pdfminer.converter import TextConverter
from pdfminer.cmapdb import CMapDB
from pdfminer.layout import LAParams

def main(fp):
    debug = 0
    pagenos = set()
    maxpages = 0
    imagewriter = None
    codec = 'utf-8'
    caching = True
    laparams = LAParams()

    PDFDocument.debug = debug
    PDFParser.debug = debug
    CMapDB.debug = debug
    PDFPageInterpreter.debug = debug

    resourceManager = PDFResourceManager(caching=caching)
    outfp = sys.stdout
    device = TextConverter(resourceManager, outfp, codec=codec, laparams=laparams, imagewriter=imagewriter)
    interpreter = PDFPageInterpreter(resourceManager, device)
    for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, caching=caching, check_extractable=True):
        interpreter.process_page(page)
    fp.close()
    device.close()
    outfp.close()
    return  # Here I want to return the extracted text string

我现在可以将其作为模块调用,如下所示:

>>> from my_pdf2txt import main
>>> main(open('myfile.pdf', 'rb'))
all the text contents
of the pdf
are printed out here..

它当前使用 sys.stdout.write() 打印出结果字符串,但我实际上希望它使用最后一行的 return 语句返回这些字符串我的代码。但由于 sys.stdout.write 的使用隐藏在 lines 165-167 in converter.py 深处,我真的不知道如何让这个方法返回这些字符串而不是将其写入标准输出。

有人知道如何让这个方法返回找到的字符串而不是将它们写入标准输出吗?欢迎所有提示!

最佳答案

根据 Darth Kotik 的建议,您可以将 sys.stdout 指向您想要的任何类似文件的对象。然后,当您调用函数时,打印的数据将定向到您的对象,而不是屏幕。示例:

import sys
import StringIO

def frob():
    sys.stdout.write("Hello, how are you doing?")


#we want to call frob, storing its output in a temporary buffer.

#hold on to the old reference to stdout so we can restore it later.
old_stdout = sys.stdout

#create a temporary buffer object, and assign it to stdout
output_buffer = StringIO.StringIO()
sys.stdout = output_buffer

frob()

#retrieve the result.
result = output_buffer.getvalue()

#restore the old value of stdout.
sys.stdout = old_stdout

print "This is the result of frob: ", result

输出:

This is the result of frob:  Hello, how are you doing?

对于您的问题,您只需将 frob() 调用替换为 main(fp) 即可。

关于python - 如何让这个Python方法返回一个字符串而不是将其写入stdout?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26507768/

相关文章:

java - For 循环缺少返回语句错误 - JAVA

python - 在 MacOSX 上为 Eclipse 多次安装 Python

python - 通过POST请求发送数据

python - tkinter 和 matplotlib Canvas 显示,但不显示导航工具栏和图形标题

node.js - 如何向 puppeteer 生成的 PDF 添加水印?

redirect - Yii2 返回 $this->goBack() 不工作

java - 方法不返回值

python - 如何在 sphinx doc 中显示实例属性?

c# - 通过单击按钮在 Windows 窗体中打开 .pdf 文件

pdf - Pandoc Markdown 到 Latex PDF : table merges rows in single row?