Python PDF parser and analyzer
PDFMiner is a suite of programs that help extracting and analyzing text data of PDF documents. Unlike other PDF-related tools, it allows to obtain the exact location of texts in a page, as well as other extra information such as font information or ruled lines. It includes a PDF converter that can transform PDF files into other text formats (such as HTML). It has an extensible PDF parser that can be used for other purpoes instead of text analysis.
Features:
Download:
http://www.unixuser.org/~euske/python/pdfminer/pdfminer-dist-20090330.tar.gz
(1.8Mbytes)
Discussion:
http://groups.google.com/group/pdfminer-users/
View the source:
http://code.google.com/p/pdfminerr/source/browse/trunk/pdfminer
Online Demonstration: (pdf -> html conversion webapp)
http://pdf2html.tabesugi.net:8080/
setup.py
to install:# python setup.py install
$ pdf2txt.py samples/simple1.pdf <html><head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> </head><body> <span style="position:absolute; border: gray 1px solid; left:0px; top:50px; width:612px; height:792px;"></span> <div style="position:absolute; top:50px;"><a name="1">Page 1</a></div> <span style="position:absolute; writing-mode:lr-tb; left:158px; top:224px; font-size:22px;"> World </span> <span style="position:absolute; writing-mode:lr-tb; left:100px; top:124px; font-size:22px;"> </span> <span style="position:absolute; writing-mode:lr-tb; left:100px; top:224px; font-size:22px;"> Hello </span> <span style="position:absolute; writing-mode:lr-tb; left:368px; top:124px; font-size:22px;">World </span> <span style="position:absolute; writing-mode:lr-tb; left:206px; top:124px; font-size:22px;">Hello </span> <div style="position:absolute; top:0px;">Page: <a href="#1">1</a></div> </body></html>
CMap
,
which is distributed from Adobe.
Here is how:
$ tar jxf CMap.tar.bz2
CMap
directory into the pdfminer
directory.
pdfminer
directory.
$ make cdbcmap
PDFMiner comes with two programs:
pdf2txt.py
and dumppdf.py
.
pdf2txt.py
extracts text contents from a PDF file.
It extracts all the texts that are to be rendered programatically.
It also extracts the corresponding locations, font names,
and font sizes for each text portion. However,
it cannot extract texts embedded within images
(i.e. it does not do optical character recognition).
You can provide a password for protected PDF documents
whose access is limited.
For non-ASCII languages, you can specify the output encoding (such as UTF-8). Note that not all characters in a PDF can be converted safely to Unicode, as some of them are not included in the current Unicode Standard.
Examples:
$ python -m pdflib.pdf2txt -o output.html samples/naacl06-shinyama.pdf (extract text as an HTML file whose filename is output.html) $ python -m pdflib.pdf2txt -c euc-jp samples/jo.pdf (extract Japanese texts in vertical writing, CMap is required) $ python -m pdflib.pdf2txt -P mypassword secret.pdf (extract texts from an encrypted PDF file with a password)
Options:
-o filename
-p pageno[,pageno,...]
-c codec
-w
-t type
html
: HTML format. (Default)
text
: TEXT format.
sgml
: SGML format.
tag
: "Tagged PDF" format. A tagged PDF has its own contents annotated with
HTML-like tags. pdf2txt tries to extract its content streams rather than inferring its text locations.
Tags used here are defined in the PDF specification (See §10.7 "Tagged PDF").
-P password
-d
dumppdf.py
dumps the internal contents of a PDF file
in pseudo-XML format. This program is primarily for debugging purpose,
but it's also possible to extract some meaningful contents
(such as images).
Examples:
$ python -m tools.dumppdf -a foo.pdf (dump all the headers and contents, except stream objects) $ python -m tools.dumppdf -T foo.pdf (dump the table of contents) $ python -m tools.dumppdf -r -i6 foo.pdf > pic.jpeg (extract a JPEG image)
Options:
-a
-p pageno
-p
options are allowed.
Note that page numbers start from one.
-r
(raw)
-b
(binary)
-t
(text)
With -r
option, all the stream contents are dumped without decoding.
With -b
option, the contents are dumped as a binary blob.
With -t
option, the contents are dumped in a text format,
similar to repr()
manner. When
-r
or -b
option is given,
no stream header is displayed for the ease of saving it to a file.
-P password
-d
Copyright (c) 2004-2009 Yusuke Shinyama <yusuke at cs dot nyu dot edu>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.