简体   繁体   English

pypdf不从pdf中提取表格

[英]pypdf not extracting tables from pdf

I am using pypdf to extract text from pdf files . 我正在使用pypdf从pdf文件中提取文本。 The problem is that the tables in the pdf files are not extracted. 问题在于pdf文件中的表未提取。 I have also tried using the pdfminer but i am having the same issue . 我也尝试过使用pdfminer,但是我遇到了同样的问题。

The problem is that tables in PDFs are generally made up of absolutely positioned lines and characters, and it is non-trivial to convert this into a sensible table representation. 问题在于,PDF中的表格通常由绝对定位的行和字符组成,并且将其转换为明智的表格表示形式并非易事。

In Python, PDFMiner is probably your best bet. 在Python中,PDFMiner可能是最好的选择。 It gives you a tree structure of layout objects, but you will have to do the table interpreting yourself by looking at the positions of lines (LTLine) and text boxes (LTTextBox). 它为您提供了布局对象的树形结构,但是您将不得不通过查看行(LTLine)和文本框(LTTextBox)的位置来解释表。 There's a little bit of documentation here . 这里有一些文档

Alternatively, PDFX attempts this (and often succeeds), but you have to use it as a web service (not ideal, but fine for the occasional job). 另外, PDFX会尝试这样做(并且通常会成功),但是您必须将其用作Web服务(不理想,但偶尔也可以使用)。 To do this from Python, you could do something like the following: 要从Python执行此操作,您可以执行以下操作:

import urllib2
import xml.etree.ElementTree as ET

# Make request to PDFX
pdfdata = open('example.pdf', 'rb').read()
request = urllib2.Request('http://pdfx.cs.man.ac.uk', pdfdata, headers={'Content-Type' : 'application/pdf'})
response = urllib2.urlopen(request).read()

# Parse the response
tree = ET.fromstring(response)
for tbox in tree.findall('.//region[@class="DoCO:TableBox"]'):
    src = ET.tostring(tbox.find('content/table'))
    info = ET.tostring(tbox.find('region[@class="TableInfo"]'))
    caption = ET.tostring(tbox.find('caption'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM