PyPDF2的替代品

时间:2018-08-27 14:41:08

标签: python python-3.x nlp pypdf2

我正在使用PyPDF2软件包从.pdf文件提取文本。我正在获取输出,但未达到所需的形式。我找不到问题所在了?

代码段如下:

import PyPDF2
def Read(startPage, endPage):
    global text
    text = []
    cleanText = " "
    pdfFileObj = open('F:\\Pen Drive 8 GB\\PDF\\Handbooks\\book1.pdf', 'rb')
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    num_pages = pdfReader.numPages
    print(num_pages)
    while (startPage <= endPage):
        pageObj = pdfReader.getPage(startPage)
        text += pageObj.extractText()
        startPage += 1
    pdfFileObj.close()
    for myWord in text:
        if myWord != '\n':
            cleanText += myWord
    text = cleanText.strip().split()
    print(text)

Read(3, 3)

我现在得到的输出作为参考附件,如下:

enter image description here

我们非常感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

此行cleanText += myWord仅将所有单词连接为一个长字符串。 如果您要过滤'\n', 代替:

for myWord in text:
        if myWord != '\n':
            cleanText += myWord
    text = cleanText.strip().split()

您可以执行以下操作:

text = [w for w in text if w != '\n']