【问题标题】:How should i extract text from pdf to csv on the basis of coordinates using python?我应该如何使用 python 根据坐标将文本从 pdf 提取到 csv?
【发布时间】:2022-12-20 14:45:03
【问题描述】:
import pdfquery
import pandas as pd

pdf = pdfquery.PDFQuery('HBL.PDF')
pdf.load()
pdf.tree.write('pdfXML.xml', pretty_print = True)

def pdfscrape(pdf):
    # Extract each relevant information individually
    shipper = pdf.pq('LTTextLineHorizontal:overlaps_bbox("33.0, 756.181, 281.4, 787.659")').text()
    consignee = pdf.pq('LTTextLineHorizontal:overlaps_bbox("33.0, 650.454, 281.4, 704.409")').text()
    notify_party = pdf.pq('LTTextLineHorizontal:overlaps_bbox("33.0, 612.909, 124.8, 621.909")').text()
    ocean_vessel = pdf.pq('LTTextLineHorizontal:overlaps_bbox("33.75, 518.409, 147.15, 527.409")').text()
    port_of_loading = pdf.pq('LTTextLineHorizontal:overlaps_bbox("164.25, 518.409, 245.25, 527.409")').text()
    port_of_discharge = pdf.pq('LTTextLineHorizontal:overlaps_bbox("33.75, 495.159, 103.95, 504.159")').text()
    place_of_delivery = pdf.pq('LTTextLineHorizontal:overlaps_bbox("164.25, 497.409, 234.45, 506.409")').text()
    for_delivery_of_goods = pdf.pq('LTTextLineHorizontal:overlaps_bbox("286.5, 549.219, 507.9, 618.159")').text()
    container_no_and_seal_no = pdf.pq('LTTextLineHorizontal:overlaps_bbox("54.75, 455.409, 92.55, 464.409")').text()
    no_of_container = pdf.pq('LTTextLineHorizontal:overlaps_bbox("164.25, 454.659, 223.65, 463.659")').text()
    gross_weight = pdf.pq('LTTextLineHorizontal:overlaps_bbox("432.75, 455.409, 475.95, 464.409")').text()
    measurement = pdf.pq('LTTextLineHorizontal:overlaps_bbox("502.5, 447.916, 534.9, 464.409")').text()
# Combined all relevant information into single observation
    page = pd.DataFrame({
                         'Shipper': shipper,
                         'Consignee': consignee,
                         'Notify_party': notify_party,
                         'Ocean_vessel': ocean_vessel,
                         'Port_of_loading': port_of_loading,
                         'port_of_discharge': port_of_discharge,
                         'place_of_delivery': place_of_delivery,
                         'for_delivery_of_goods': for_delivery_of_goods,
                         'container_no_and_seal_no': container_no_and_seal_no,
                         'no_of_container': no_of_container,
                         'gross_weight': gross_weight,
                         'measurement': measurement
                       }, index=[0])
    return(page)

pagecount = pdf.doc.catalog['Pages'].resolve()['Count']
master = pd.DataFrame()
for p in range(pagecount):
    pdf.load(p)
    page = pdfscrape(pdf)
    master = master.append(page, ignore_index=True)

master.to_csv('output.csv', index=False)

`

`

将文本转换为 xml 文件后,我提取了 CSV 格式的文本。而且,在坐标的基础上,我将其转换为 csv。但是,我在这里手动检查 xml 后放置坐标。因为,其他 pdf 可能会有所不同。所以,我想动态使用坐标(直接来自 xml 文件)。

【问题讨论】:

  • 什么的坐标?你的代码行 ```master = master.append(page, ignore_index=True)''' 的结果是什么?

标签: python ocr export-to-csv python-tesseract text-extraction


【解决方案1】:
猜你喜欢
  • 2021-01-07
  • 2011-09-05
  • 1970-01-01
  • 2014-05-18
  • 1970-01-01
  • 2011-11-16
  • 2023-03-11
  • 2022-10-15
  • 1970-01-01
相关资源
最近更新 更多