0

我想将pdf转换为文本。我在 python 命令提示符下尝试了这段代码,但没有显示任何输出。也许我错了。你能告诉我我哪里错了吗?提前致谢。

import pyPdf

def getPDFContent(path):
    content = ""
    # Load PDF into pyPDF
    pdf = pyPdf.PdfFileReader(file(path, "rb"))
    # Iterate pages
    for i in range(0, pdf.getNumPages()):
        # Extract text from page and add to content
        content += pdf.getPage(i).extractText() + "\n"
    # Collapse whitespace
    content = " ".join(content.replace(u"\xa0", " ").strip().split())
    return content

print getPDFContent("test.pdf").encode("ascii", "ignore")
4

1 回答 1

2

如果您的 PDF 仅包含图像(例如来自扫描页面),那么您将无法提取任何文本。

于 2011-05-24T07:47:48.693 回答