我有一个包含表格、文本和一些图像的PDF。我想在PDF中的任何位置提取表
现在我正在手动从页面中查找表。从那里,我捕获该页面并保存到另一个PDF
import PyPDF2
PDFfilename = "Sammamish.pdf" #filename of your PDF/directory where your PDF is stored
pfr = PyPDF2.PdfFileReader(open(PDFfilename, "rb")) #PdfFileReader object
pg4 = pfr.getPage(126) #extract pg 127
writer = PyPDF2.PdfFileWriter() #create PdfFileWriter object
#add pages
writer.addPage(pg4)
NewPDFfilename = "allTables.pdf" #filename of your PDF/directory where you want your new PDF to be
with open(NewPDFfilename, "wb") as outputStream:
writer.write(outputStream) #write pages to new PDF
我的目标是从整个PDF文档中提取表
如果您的pdf是基于文本的,而不是扫描的文档(即,如果您可以在pdf查看器中单击并拖动以选择表格中的文本),则您可以将模块^{} 用于
然后,您可以选择如何保存表(作为csv、json、excel、html、sqlite),以及是否应在ZIP存档中压缩输出
编辑:^{} 的显示速度大约是
camelot-py
的6倍,因此应该改用它给予
这是我提取pdf的代码
有关更多详情,请参阅我的repo
这个答案适用于任何遇到带有图像的PDF并需要使用OCR的人。我找不到可行的现成解决方案;没有什么能给我提供我所需要的准确度
以下是我发现有效的步骤
使用https://poppler.freedesktop.org/中的
pdfimages
将pdf页面转换为图像使用Tesseract检测旋转,使用ImageMagick{}修复旋转
使用OpenCV查找和提取表
使用OpenCV查找并从表中提取每个单元格
使用OpenCV对每个单元格进行裁剪和清理,这样就不会有干扰OCR软件的噪音
使用Tesseract对每个单元格进行OCR
将每个单元格的提取文本合并为所需的格式
我编写了一个python包,其中的模块可以帮助完成这些步骤
回购:https://github.com/eihli/image-table-ocr
文件及;资料来源:https://eihli.github.io/image-table-ocr/pdf_table_extraction_and_ocr.html
有些步骤不需要代码,它们利用了
pdfimages
和tesseract
等外部工具。我将为确实需要代码的两个步骤提供一些简短的示例在了解如何查找表时,此链接是一个很好的参考https://answers.opencv.org/question/63847/how-to-extract-tables-from-an-image/
这与2非常相似,因此我不会包含所有代码。我将参考的部分是对单元格进行排序
我们想从左到右,从上到下识别细胞
我们将找到最左上角的矩形。然后我们将找到所有中心位于左上角矩形上y和下y值范围内的矩形。然后我们将根据矩形中心的x值对其进行排序。我们将从列表中删除这些矩形并重复
相关问题 更多 >
编程相关推荐