Python 尝试循环浏览多个PDF文件并在两个搜索条件之间提取文本

Python 尝试循环浏览多个PDF文件并在两个搜索条件之间提取文本,python,python-3.x,Python,Python 3.x,我试图查看多个PDF文件,查看每个文件的文本,并提取(开始)“注释1-组织”和“注释2-组织”(结束)之间的段落。每个文件在这里都有不同的文本,我想打印每个文件中的每个段落,或者将段落保存到文本文件中 下面,我编写了一个小脚本,它打开一个文件,找到一个文本字符串,然后打印找到文本的页面。我认为这是一个很好的开始,但我真的想循环浏览许多PDF文件,查找特定的文本体,并将找到的所有内容保存到单个文本文件中 import PyPDF2 import re # open the pdf file ob

我试图查看多个PDF文件,查看每个文件的文本,并提取(开始)“注释1-组织”和“注释2-组织”(结束)之间的段落。每个文件在这里都有不同的文本,我想打印每个文件中的每个段落,或者将段落保存到文本文件中

下面,我编写了一个小脚本,它打开一个文件,找到一个文本字符串,然后打印找到文本的页面。我认为这是一个很好的开始,但我真的想循环浏览许多PDF文件,查找特定的文本体,并将找到的所有内容保存到单个文本文件中

import PyPDF2
import re

# open the pdf file
object = PyPDF2.PdfFileReader("C:/my_path/file1.pdf")

# get number of pages
NumPages = object.getNumPages()

# define keyterms
String = "New York State Real Property Law"

# extract text and do the search
for i in range(0, NumPages):
    PageObj = object.getPage(i)
    print("this is page " + str(i)) 
    Text = PageObj.extractText() 
    # print(Text)
    ResSearch = re.search(String, Text)
    print(ResSearch)

非常感谢您对解决此问题的任何见解

如果您的文件名类似于file1.pdf、file2.pdf和。。。然后可以使用for循环:

import PyPDF2
import re

for k in range(1,100):
    # open the pdf file
    object = PyPDF2.PdfFileReader("C:/my_path/file%s.pdf"%(k))

    # get number of pages
    NumPages = object.getNumPages()

    # define keyterms
    String = "New York State Real Property Law"

    # extract text and do the search
    for i in range(0, NumPages):
        PageObj = object.getPage(i)
        print("this is page " + str(i)) 
        Text = PageObj.extractText() 
        # print(Text)
        ResSearch = re.search(String, Text)
        print(ResSearch)
否则,您可以使用操作系统模块浏览文件夹

import PyPDF2
import re
import os

for foldername,subfolders,files in os.walk(r"C:/my_path"):
    for file in files:
        # open the pdf file
        object = PyPDF2.PdfFileReader(os.path.join(foldername,file))

        # get number of pages
        NumPages = object.getNumPages()

        # define keyterms
        String = "New York State Real Property Law"

        # extract text and do the search
        for i in range(0, NumPages):
            PageObj = object.getPage(i)
            print("this is page " + str(i)) 
            Text = PageObj.extractText() 
            # print(Text)
            ResSearch = re.search(String, Text)
            print(ResSearch)
对不起,如果我把你的问题认错了

编辑:

不幸的是,我不熟悉pyPDF2模块,但当您使用该模块转换pdf内容时,似乎会发生一些奇怪的事情(如额外的换行符或格式更改或…)

希望本页有助于:

然而,如果您的文件是.txt,那么正则表达式是有用的

import re
import os
myRegex=re.compile("New York State Real Property Law.*?common elements of the property\.",re.DOTALL)
for foldername,subfolders,files in os.walk(r"C:/Users/Mirana/Me2"):
    for file in files:
        object=open(os.path.join(foldername,file))
        Text=object.read()
        for subText in myRegex.findall(Text):
            print(subText)

object.close()
我也更改了您的pdf版本,但上述问题的原因至少对我的pdf不起作用(尝试一下):


你到底有什么问题?听起来你在试图找到一种优化/提高处理时间的方法。如果是这样,您可能需要检查多处理模块:不,时间不是问题。我不在乎这是几秒钟还是几个小时(我怀疑这会花这么长时间)。我想循环浏览多个PDF文件,并在起点和终点之间提取文本。现在,我的代码查看1个文件和1个字符串。我想看看N个文件和2个字符串。谢谢。这解决了我的部分问题。谢谢现在,当我循环浏览所有文件时,我想拉出一个字符串段落,以“纽约州不动产法”开头,以“财产的公共元素”结尾。我想打印这些锚之间的所有文本,包括这些锚。我该怎么做?是的,第一个有效!基本上,我在尝试读入所有PDF文件时遇到了此错误:“charmap”编解码器无法将389:character maps中的字节0x90解码为“当我将所有PDF转换为文本文件时,一切正常。至于第二个,它所做的只是打印“这是第0页”…“这是第11页”。那很好。我有一个工作的解决方案,这是我所需要的!非常感谢!!
import PyPDF2
import re
import os

myRegex=re.compile("New York State Real Property Law.*?common elements of the property\.",re.DOTALL)
for foldername,subfolders,files in os.walk(r"C:/my_path"):
    for file in files:
        # open the pdf file
        object = PyPDF2.PdfFileReader(os.path.join(foldername,file))

        # get number of pages
        NumPages = object.getNumPages()

        # extract text and do the search
        for i in range(0, NumPages):
            PageObj = object.getPage(i)
            print("this is page " + str(i)) 
            Text = PageObj.extractText() 
            # print(Text)
        for subText in myRegex.findall(Text):
            print(subText)