我使用pypdf从pdf文件中提取文本。问题是pdf文件中的表没有被提取出来。我也尝试过使用pdfminer,但我遇到了同样的问题。
发布于 2013-07-13 00:02:06
问题是,PDF中的表格通常由绝对定位的线条和字符组成,将其转换为合理的表格表示并不是一件容易的事情。
在Python中,PDFMiner可能是您最好的选择。它为您提供了布局对象的树形结构,但您必须通过查看线条(LTLine)和文本框(LTTextBox)的位置来解释表格。There's a little bit of documentation here。
或者,PDFX会尝试这样做(通常都会成功),但您必须将其用作web服务(不是很理想,但对于偶尔的工作来说很好)。要在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'))https://stackoverflow.com/questions/17523193
复制相似问题