社区成员您好,
我想从一本以 .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()
这是一个可能的解决方案:
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(要读取的第一页,要读取的最后一页)
Note:读取第一页是从 0 开始,而不是从 1 开始(例如在数组中)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)