有没有更好的方法从pdf中提取数据?

问题描述

我正在尝试自动化从我的银行对帐单中提取数据的过程,并将它们导入到 excel 中。我知道有银行 API 可以帮助直接获取这些数据,但对于这个用例,我不能使用它们。

我正在使用 python3PyPDF2 库。 (我使用 python3 是因为它是我最熟悉的语言,但如果有更适合我的用例的语言,我愿意尝试任何事情)

我到现在为止的代码

# Importing required modules
import PyPDF2

def pdf_to_excel():
    # Creating a pdf file object
    pdfFileObj = open('Horses Jan 2020.pdf','rb')
    
    # Creating a pdf reader object
    pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
    
    # Printing number of pages in pdf file
    print(pdfReader.numPages)

    # Printing Document Layout
    print(pdfReader.pageLayout)
    
    # Creating a page object
    pageObj = pdfReader.getPage(0)

    # Extract movement details into a dict
    movments = extract_movement_details(pageObj)

    
    # Closing the pdf file object
    pdfFileObj.close()
    
    return movments

def extract_movement_details(pageObj):
    
    # Extracting text from page
    pdftext = pageObj.extractText()

    # Split pdf text
    text_split = pdftext.split()

    """ Extract General information from Bank Statment """
    # Extract General information
    client_n = text_split[10].replace('Cliente','').split('R',1)
    general_info = {
        'From': text_split[1],'To': text_split[3].replace('Fecha',''),'Account No.': text_split[8].replace('Cuenta','').replace('No.','Client No.': client_n[0],'R.F.C': client_n[1].replace('.F.C','No. Key Account': text_split[12].replace('CLABE','').replace('PAGINA','')
        }
    
    # Extract movments information

    movement_details = {
        'Date Opt': 0,'Date Liq': 0,'Description': 0,'Reference': 0,'Debit': 0,'Credit': 0,'Operation': 0,'Liquidation': 0
        }

    print(general_info)
    print(movement_details)
    print("\n")
    
    # Printing the text from the page
    print(text_split)
    
    movments = text_split
    return movments


pdf_to_excel()

它使用 PyPDF2 并由两个函数组成,一个用于打开文件,另一个用于拆分文本并提取我需要的数据。 基本上,我试图识别模式并根据这些模式提取数据。然而,事实证明这比我想象的要困难得多,因为我得到的列表中的数据似乎没有以合乎逻辑的方式组织。单词粘在一起,它们的顺序与它们在 excel 上的视觉顺序不同,事实证明,识别模式非常困难。

不幸的是,由于它是银行信息,我不确定如何在不泄露敏感信息的情况下在此处显示它,但我会努力解决这个问题并尝试发布提取的文本和文档的外观。现在这是一张敏感信息已被编辑的图片

enter image description here

此外,在尝试使用 PyPDF2 之前,我尝试使用了 tabula,但由于移动是在一个带有不可见边框的表格中,因此无法将它们识别为表格。

此外,未来我将不得不与更多银行合作,所以如果我想要的东西可以通过具有更大学习曲线的技术来完成,我对此表示同意。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...