Skip to main content
 首页 » 编程设计

python之未从 PyPDF2 上的 PDF 正则表达式接收到正确的模式

2025年05月04日23飞鱼

我想从 PDF 中提取特定单词的所有实例,例如“数学”。 到目前为止,我正在使用 PyPDF2 将 PDF 转换为文本,然后对其执行正则表达式以找到我想要的内容。这是example PFD

当我运行我的代码而不是返回我的“数学”正则表达式模式时,它返回整个页面的字符串。请帮忙谢谢

#First Change Current Working Directory to desktop 
 
import os 
os.chdir('/Users/Hussein/Desktop')         #File is located on Desktop 
 
 
#Second is the PyPDF2 
 
pdfFileObj=open('TEST1.pdf','rb')          #Opening the File 
pdfReader=PyPDF2.PdfFileReader(pdfFileObj) 
pageObj=pdfReader.getPage(3)               #For the test I only need page 3 
TextVersion=pageObj.extractText() 
print(TextVersion) 
 
 
 
#Third is the Regular Expression 
 
import re 
match=re.findall(r'math',TextVersion) 
for match in TextVersion: 
      print(match) 

我收到的不仅仅是“数学”的所有实例:

I 
n 
t 
r 
o 
d 
u 
c 
t 
i 
o 
n 

等等等等

请您参考如下方法:

TextVersion 变量保存文本。当您将它用于 for 循环时,它会一次给您文本一个字符,如您所见。 findall 函数将返回一个匹配列表,因此如果您将它用于您的 for 循环,您将得到每个单词(在您的测试中它们都是相同的) .

import re 
 
for match in re.findall(r'math',TextVersion): 
      print(match) 

findall 的返回结果类似于:

["math", "math", "math"] 

所以你的输出将是:

math 
math 
math