从PDF中提取表的问题

时间:2015-03-03 09:18:18

标签: pdf web-scraping scraperwiki tabula

我知道这个主题有几个主题,但他们的解决方案似乎都不适用于我。我在PDF document中有一个表格,我希望能够从中提取信息。我可以将文本复制并粘贴到textedit中,它清晰可辨,但不是真的可用。我的意思是所有文本都是可读的,但数据都是用空格分隔的,无法区分列中单元格与文本内的空格。

但是每当我尝试使用像tabula或scraper wiki这样的工具时,提取的文本就是垃圾。

有人能够就如何解决这个问题给我任何指示吗?

2 个答案:

答案 0 :(得分:0)

这是使用Python和Unix的解决方案

在Python中:

import urllib
# download pdf
testfile = urllib.URLopener()
testfile.retrieve('http://www.european-athletics.org/mm/Document/EventsMeetings/General/01/27/52/10/EICH-FinalEntriesforwebsite_Neutral.pdf', 'test.pdf')

在Unix中:

$ pdftotext -layout test.pdf

输出到test.txt的片段:

Lastname Firstname Country DOB PB SB 1500m Men Rowe Brenton AUT 17/08/1987 Vojta Andreas AUT 09/06/1989 3:38.99 3:41.09 Khadiri Amine CYP 20/11/1988 3:45.16 3:45.16 Friš Jan CZE 19/12/1995 3:43.76 3:43.76 Holuša Jakub CZE 20/02/1988 3:38.79 3:41.54 Kocourek Milan CZE 06/12/1987 3:43.97 3:43.97 Bueno Andreas DEN 07/07/1988 3:42.78 3:42.78 Alcalá Marc ESP 07/11/1994 3:41.79 3:41.79 Mechaal Adel ESP 05/12/1990 3:38.30 3:38.30 Olmedo Manuel ESP 17/05/1983 3:39.82 3:40.66 Ruíz Diego ESP 05/02/1982 3:36.42 3:40.60 Kowal Yoann FRA 28/05/1987 3:38.07 3:39.22 Grice Charlie GBR 07/11/1993 3:39.44 3:39.44 O'Hare Chris GBR 23/11/1990 3:37.25 3:40.42 Orth Florian GER 24/07/1989 3:39.97 3:40.20 Tesfaye Homiyu GER 23/06/1993 3:34.13 3:34.13 Kazi Tamás HUN 16/05/1985 3:44.28 3:44.28 Mooney Danny IRL 20/06/1988 3:42.69 3:42.69 Travers John IRL 16/03/1991 3:42.52 3:43.74 Bussotti Neves Junior Joao Capistrano M. ITA 10/05/1993 3:47.58 3:47.58 Jurkēvičs Dmitrijs LAT 07/01/1987 3:45.95 3:45.95 Ingebrigtsen Henrik NOR 24/02/1991 3:44.00 Ingebrigtsen Filip NOR 20/04/1993 Krawczyk Szymon POL 29/12/1988 3:41.64 3:41.64 Ostrowski Artur POL 10/07/1988 3:41.36 3:41.36 ebrowski Krzysztof POL 09/07/1990 3:41.49 3:41.49 Smirnov Valentin RUS 13/02/1986 3:37.55 3:38.74 Nava Goran SRB 15/04/1981 3:40.65 3:44.49 Pelikán Jozef SVK 29/07/1984 3:43.85 3:45.51 Ek Staffan SWE 13/11/1991 3:43.54 3:43.54 Rogestedt Johan SWE 27/01/1993 3:40.03 3:40.03 Özbilen lham Tanui TUR 05/03/1990 3:34.76 3:38.05 Özdemir Ramazan TUR 06/07/1991 3:44.35 3:44.35

答案 1 :(得分:0)

您还可以下载一个简单的命令行工具来处理您链接到的PDF文件。运行此命令以在第一页上提取表:

pdftotext     \
   -enc UTF-8 \
   -l 1       \
   -table     \
    EICH-FinalEntriesforwebsite_Neutral.pdf \
    EICH-FinalEntriesforwebsite_Neutral.txt
  • -enc UTF-8 :设置文本编码,以便Ö,Ä,Ü和İ(以及ö,ä,ü,ß,á,š,ē,í和č)正确提取文本中的字符。
  • -l 1 :告诉命令将页码1提取为最后一页。
  • -table :这是决定性的参数。

该命令产生此输出:

EUROPEAN ATHLETICS INDOOR CHAMPIONSHIPS PRAGUE / CZE, 6-8 MARCH 2015 FINAL ENTRIES - MEN Lastname Firstname Country DOB PB SB 1500m Men Rowe Brenton AUT 17/08/1987 Vojta Andreas AUT 09/06/1989 3:38.99 3:41.09 Khadiri Amine CYP 20/11/1988 3:45.16 3:45.16 Friš Jan CZE 19/12/1995 3:43.76 3:43.76 Holuša Jakub CZE 20/02/1988 3:38.79 3:41.54 Kocourek Milan CZE 06/12/1987 3:43.97 3:43.97 Bueno Andreas DEN 07/07/1988 3:42.78 3:42.78 Alcalá Marc ESP 07/11/1994 3:41.79 3:41.79 Mechaal Adel ESP 05/12/1990 3:38.30 3:38.30 Olmedo Manuel ESP 17/05/1983 3:39.82 3:40.66 Ruíz Diego ESP 05/02/1982 3:36.42 3:40.60 Kowal Yoann FRA 28/05/1987 3:38.07 3:39.22 Grice Charlie GBR 07/11/1993 3:39.44 3:39.44 O'Hare Chris GBR 23/11/1990 3:37.25 3:40.42 Orth Florian GER 24/07/1989 3:39.97 3:40.20 Tesfaye Homiyu GER 23/06/1993 3:34.13 3:34.13 Kazi Tamás HUN 16/05/1985 3:44.28 3:44.28 Mooney Danny IRL 20/06/1988 3:42.69 3:42.69 Travers John IRL 16/03/1991 3:42.52 3:43.74 Bussotti Neves Junior Joao Capistrano M. ITA 10/05/1993 3:47.58 3:47.58 Jurkēvičs Dmitrijs LAT 07/01/1987 3:45.95 3:45.95 Ingebrigtsen Henrik NOR 24/02/1991 3:44.00 Ingebrigtsen Filip NOR 20/04/1993 Krawczyk Szymon POL 29/12/1988 3:41.64 3:41.64 Ostrowski Artur POL 10/07/1988 3:41.36 3:41.36 Żebrowski Krzysztof POL 09/07/1990 3:41.49 3:41.49 Smirnov Valentin RUS 13/02/1986 3:37.55 3:38.74 Nava Goran SRB 15/04/1981 3:40.65 3:44.49 Pelikán Jozef SVK 29/07/1984 3:43.85 3:45.51 Ek Staffan SWE 13/11/1991 3:43.54 3:43.54 Rogestedt Johan SWE 27/01/1993 3:40.03 3:40.03 Özbilen İlham Tanui TUR 05/03/1990 3:34.76 3:38.05 Özdemir Ramazan TUR 06/07/1991 3:44.35 3:44.35 3000m Men Rowe Brenton AUT 17/08/1987 Vojta Andreas AUT 09/06/1989 7:59.95 7:59.95

但请注意:

pdftotext 命令行工具的 -table 参数仅适用于XPDF版本3.04,您可以使用点击此处下载: www.foolabs.com/xpdf/download.html 。在 pdftotext (其最新版本为0.43.0)的Poppler分支中可用 NOT (尚未)。

如果您只有Poppler的 pdftotext ,则必须使用 -layout 参数(而不是 -table ),它为您提供了相同的PDF文件效果:

pdftotext     \
   -enc UTF-8 \
   -l 1       \
   -layout    \
    EICH-FinalEntriesforwebsite_Neutral.pdf \
    EICH-FinalEntriesforwebsite_Neutral.txt

但是,我看过PDF -table (和XPDF)的结果比使用 {更好{1}} (和Poppler)。

(XPDF也有 -layout 参数 - 所以你可以如果您同时尝试两者,请看差异。)