如何在路径中遍历PDF文件、格式化和清理每个文件,并从单个文件中吐出带有特定文本的regex?

2024-04-20 05:21:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个脚本,需要一个PDF格式的HTML格式,清理了HTML标签,并吐出一个干净的文本。然后运行一些regex从每个PDF中提取数据。基本上,我不知道如何遍历所有文件并运行清理,然后为每个文件运行regex。我的代码如下所示:

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import HTMLConverter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from bs4 import BeautifulSoup
from cStringIO import StringIO
from pandas import DataFrame
import pandas as pd
import glob
import re

path = r'F:\Desktop\Metadata\'
allFiles = glob.glob(path + "/*.pdf")
for file_ in allFiles:
    convert_pdf_to_html(file_)

def convert_pdf_to_html(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = HTMLConverter(rsrcmgr, retstr, codec = codec, laparams = laparams)
    fp = file(path, 'rb')
    interpreter = PDFPageInterpreter(rsrcmgr, device)
    password = ""
    maxpages = 0
    caching = True
    pagenos = set()
    for page in PDFPage.get_pages(fp, pagenos, maxpages = maxpages, password = password, caching = caching, check_extractable = True):
        interpreter.process_page(page)
    fp.close()
    device.close()
    str = retstr.getvalue()
    retstr.close()
    return str

这是我困惑的部分。我必须将convert\u pdf\u to\u html设置为一个变量,例如“text”,然后获取该文本并通过beautiful soup将其输入以进行清理。我需要这样做的每个PDF文件在我的文件夹。你知道吗

text = convert_pdf_to_html(path)
soup = BeautifulSoup(text, 'lxml')  #remove HTML tags
document_text = soup.get_text()     #cleaned up text

然后我需要运行一些像这样的regex查询,并将它们输出到一个格式为文件名.csv'

print " Alt : "
list = (re.findall(r"""
    ((?:LE|BA|BE|BM|BC)[\w]+\:)
    """,document_text, re.X))

print list

Tags: 文件topathtextfromimportconvertpdf
1条回答
网友
1楼 · 发布于 2024-04-20 05:21:39

好的方法是创建一个类,该类可以处理发送给该类的每个文件实例的每个函数。你知道吗

Class PDFParser:
    file = ''
    def __init__(self, myfile):
          file = myfile

    def get_html_response(self):
        //your code for pdf to html

    def run_regx(self):
        //run your regex here.

for file_ in allFiles:
    my_parse = PDFParser(file_)
    my_parse.get_html_response()
    my_parse.run_regx()

相关问题 更多 >