PDF数据和表格刮到Excel

我试图找出一个很好的方法来提高我的数据录入工作的生产力。

我正在做的是想办法从PDF中抓取数据并将其input到Excel中。

更具体地说,我正在使用的数据来自杂货店传单。 现在,我们必须手动input传单中的每一笔交易到数据库中。 传单样本是http://weeklyspecials.safeway.com/customer_Frame.jsp?drpStoreID=1551

我希望做的是有产品,价格和预定义的选项(忠诚卡,优惠券,select品种…等)的列。

任何帮助将不胜感激,如果我需要更具体让我知道。

在查看OP链接的特定PDF之后,我不得不说,这并不是一个典型的表格格式。

它包含“单元格”内部的许多图像,但单元格并非全部严格垂直或水平alignment:

来自OP中链接的PDF

所以这甚至不是一个“好”的桌子,而是一个非常丑陋和尴尬的桌子。


话虽如此,我必须补充:

一般从PDF 提取“好”的表格是非常困难的…

标准PDF不提供任何关于它们在页面上绘制的语义的提示:语法提供的唯一区别是向量元素(线条,填充…),图像和文本之间的区别。

不pipe是任何字符是表格的一部分还是行的一部分,或者只是一个孤独的单个字符,在其他空白的区域内,通过parsingPDF源代码都不容易通过编程来识别。

有关为什么PDF文件格式永远不会被认为适合托pipe可抽取的结构化数据的背景 ,请参阅以下文章:

为什么更新文件是如此困难 (ProPublica-Website)

…但TabulaPDF这样做的工作非常好!

说了上面现在让我补充一点:

  • 对于一个惊人的开源工具系列,每周都会从PDF中提取表格数据(除非是扫描的页面),这些工具每周都会变得越来越好, 这与我在引入段落中所说的内容相矛盾 ! – 检查TabulaPDF 。 看到这些链接:

    • Tabula简介:上载PDF,取回表格CSV数据。 噗!
    • Tabula-Extractor:Tabula的命令行界面
    • Tabula源代码库
    • Tabula API(即将到来,还没有准备好)

Tabula-Extractor是用Ruby编写的。 在后台使用PDFBox(用Java编写)和一些其他的第三方库。 要运行,Tabula-Extractor需要安装JRuby-1.7。


安装Tabula提取器

我直接从GitHub源代码库使用Tabula-Extractor的“最新版”版本。 让它工作是非常容易的,因为在我的系统上JRuby-1.7.4_0已经存在:

mkdir ~/svn-stuff cd ~/svn-stuff git clone https://github.com/tabulapdf/tabula-extractor.git git.tabula-extractor 

包含在这个Git克隆将已经是所需的库,所以不需要安装PDFBox。 命令行工具位于/bin/子目录中。

浏览命令行选项:

 ~/svn-stuff/git.tabula-extractor/bin/tabula -h Tabula helps you extract tables from PDFs Usage: tabula [options] <pdf_file> where [options] are: --pages, -p <s>: Comma separated list of ranges, or all. Examples: --pages 1-3,5-7, --pages 3 or --pages all. Default is --pages 1 (default: 1) --area, -a <s>: Portion of the page to analyze (top,left,bottom,right). Example: --area 269.875,12.75,790.5,561. Default is entire page --columns, -c <s>: X coordinates of column boundaries. Example --columns 10.1,20.2,30.3 --password, -s <s>: Password to decrypt document. Default is empty (default: ) --guess, -g: Guess the portion of the page to analyze per page. --debug, -d: Print detected table areas instead of processing. --format, -f <s>: Output format (CSV,TSV,HTML,JSON) (default: CSV) --outfile, -o <s>: Write output to <file> instead of STDOUT (default: -) --spreadsheet, -r: Force PDF to be extracted using spreadsheet-style extraction (if there are ruling lines separating each cell, as in a PDF of an Excel spreadsheet) --no-spreadsheet, -n: Force PDF not to be extracted using spreadsheet-style extraction (if there are ruling lines separating each cell, as in a PDF of an Excel spreadsheet) --silent, -i: Suppress all stderr output. --use-line-returns, -u: Use embedded line returns in cells. (Only in spreadsheet mode.) --version, -v: Print version and exit --help, -h: Show this message 

提取OP要的表

我甚至没有试图从OP的怪物PDF中提取这个丑陋的表格。 我会把它作为一个练习足够的冒险这些读者的运动…

相反,我将演示如何提取一个“好”的表。 我将从官方PDF-1.7规范中获取第651-653页,这里用截图表示:

官方PDF-1.7规范的第651-653页

我用这个命令:

  ~/svn-stuff/git.tabula-extractor/bin/tabula \ -p 651,652,653 -g -n -u -f CSV \ ~/Downloads/pdfs/PDF32000_2008.pdf 

将生成的CSV导入LibreOffice Calc后,电子表格如下所示:

导入CSV后从LibreOffice截图

对我来说,这看起来像是一个表格的完美提取,它分布在3个不同的PDF页面上。 (即使表格单元格中使用的换行符也将其纳入电子表格中。)


更新

这里是一个ASCiinema屏幕录像(你也可以在asciinema命令行工具的帮助下,在你的Linux / MacOSX / Unixterminal上本地下载和重新播放),由tabula-extractor主演:

asciicast