你好,社区成员,
我想从一本以.pdf作为文件扩展名的电子书中提取所有文本。我逐渐了解到python有一个包PyPDF2来执行必要的操作。不知何故,我尝试过并能够提取文本,但它会导致提取词之间的空间不适当,有时结果是2-3合并词的结果。
此外,我想从第3页继续提取文本,因为最初的页涉及封面和序言。另外,我不想包含最后5页,因为它包含术语表和索引。
是否有其他方法可以读取没有加密的.pdf二进制文件?
代码片段,无论我到现在尝试了什么,如下所示。
import PyPDF2
def Read():
pdfFileObj = open('book1.pdf','rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
#discerning the number of pages will allow us to parse through all #the pages
num_pages = pdfReader.numPages
count = 0
global text
text = []
while(count < num_pages):
pageObj = pdfReader.getPage(count)
count +=1
text += pageObj.extractText().split()
print(text)
Read()发布于 2018-08-24 20:58:40
这是一个可能的解决办法:
import PyPDF2
def Read(startPage, endPage):
global text
text = []
cleanText = ""
pdfFileObj = open('myTest2.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
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.split()
print(text)
Read(0,0)Read()参数--> read (第一页要读,最后一页要读)
注意:读取第一页的从0开始,而不是从1开始(例如在数组中)。
https://stackoverflow.com/questions/52009695
复制相似问题