【问题标题】:Python PDF ScrapingPython PDF 抓取
【发布时间】:2021-06-30 12:53:42
【问题描述】:

任务:

PDF 是银行对账单,包含列,即(日期、描述、存款、取款、余额),用各自的字段解析列并以 CSV 格式导出该数据。PDF

我的代码:

import pdftotext
import re
import csv

# open PDF file
with open('test.pdf', 'rb') as pdf_file:
pdf = pdftotext.PDF(pdf_file)

# extract tabular text
lines = pdf[2].split('\n')[4:]
# CSV table
table = []

# loop over lines in table
for line in lines:
# replace trailing spaces with comas
row = re.sub('   ', ',', line)

# reducing the number of comas to one
row = [cols.strip() for cols in re.sub(',+', ',', row).split(',')]

# handling missed separators
row = ','.join(row).replace('  ', ',').split(',')

# append row to table
table.append(row)

print(table)

# write CSV output
with open('test.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerows(table)

问题:

我没有得到想要的输出,即一半的描述显示在日期表下。我附上 csv 以供进一步参考 here

期望的输出:

例如

['04/02','克莱斯勒资本支付 0023582513','$469.88-','$51.15']

【问题讨论】:

    标签: python pdf-scraping


    【解决方案1】:

    Example of output

    你可以使用 pdfplumber 库,它非常有用,我在五分钟内得到这个输出,它需要使用表格参数

    import pandas as pd
    import pdfplumber
    pdf = pdfplumber.open(r'C:\Users\Erkin\Downloads\test.pdf')
    df = pd.DataFrame()
    table_settings={"vertical_strategy": "text", 
        "horizontal_strategy": "lines","intersection_y_tolerance": 8}
    df = pd.DataFrame(pdf.pages[3].extract_table(table_settings))
    df.to_csv(r'C:\Users\Erkin\Downloads\test.csv')
    

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-18
      • 1970-01-01
      • 2022-09-26
      • 2021-06-14
      • 1970-01-01
      相关资源
      最近更新 更多