PDF数据和表格刮擦到Excel

Cas*_*ers 11 pdf ocr excel screen-scraping

我正试图找到一种提高数据录入作业效率的好方法.

我想要做的是想出一种从PDF中抓取数据并将其输入Excel的方法.

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

我希望做的是有产品,价格和预定义选项的列(会员卡,优惠券,选择多种......那种东西).

任何帮助将不胜感激,如果我需要更具体,请告诉我.

Kur*_*fle 21

在查看由OP链接的特定PDF之后,我不得不说这并不完全显示典型的表格格式.

它在"单元格"中包含许多图像,但单元格并非都严格垂直或水平对齐:

来自OP中链接的PDF页面

所以这甚至不是一个"漂亮"的桌子,而是一个非常丑陋和尴尬的工作...


话虽如此,我还要补充一下:

从PDF 提取甚至"漂亮"的表格非常困难......

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

通过解析PDF源代码,通过编程方式无法识别任何字符是表的一部分还是一部分行,或者只是一个孤独的单个字符在其他空白区域内.

有关为什么PDF文件格式永远不应该被认为适合托管可提取的结构化数据的背景,请参阅此文章:

为什么更新文档的原因是如此困难(ProPublica-网站)

...但使用TabulaPDF这样做非常好!

说完上面的内容后我就加上这个:

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


安装Tabula-Extractor

我直接从其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
Run Code Online (Sandbox Code Playgroud)

此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
Run Code Online (Sandbox Code Playgroud)

提取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
Run Code Online (Sandbox Code Playgroud)

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

导入CSV后LibreOffice的屏幕截图

对我来说,这看起来像一张桌子的完美提取,它分布在3个不同的PDF页面上.(甚至表格单元格中使用的换行也会进入电子表格.)


更新

这是一个ASCiinema截屏视频(您可以在命令行工具的帮助下在Linux/MacOSX/Unix终端上本地下载和重新播放asciinema),主演tabula-extractor:

asciicast

  • 读起来像广告一样 (2认同)