Python PDF parser and analyzer
Homepage Recent Changes PDFMiner API
PDFMiner is a tool for extracting information from PDF documents. Unlike other PDF-related tools, it focuses entirely on getting and analyzing text data. PDFMiner allows to obtain the exact location of texts in a page, as well as other information such as fonts or 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 purposes instead of text analysis.
PDFMiner is about 20 times slower than other C/C++-based counterparts such as XPdf.
Online Demo: (pdf -> html conversion webapp)
http://pdf2html.tabesugi.net:8080/
Source distribution:
http://pypi.python.org/pypi/pdfminer/
github:
https://github.com/euske/pdfminer/
Questions and comments:
http://groups.google.com/group/pdfminer-users/
setup.py
to install:# python setup.py install
$ pdf2txt.py samples/simple1.pdf Hello World Hello World H e l l o W o r l d H e l l o W o r l d
In order to process CJK languages, you need an additional step to take during installation:
# make cmap python tools/conv_cmap.py pdfminer/cmap Adobe-CNS1 cmaprsrc/cid2code_Adobe_CNS1.txt cp950 big5 reading 'cmaprsrc/cid2code_Adobe_CNS1.txt'... writing 'CNS1_H.py'... ... (this may take several minutes) # python setup.py install
On Windows machines which don't have make
command,
paste the following commands on a command line prompt:
python tools\conv_cmap.py pdfminer\cmap Adobe-CNS1 cmaprsrc\cid2code_Adobe_CNS1.txt cp950 big5 python tools\conv_cmap.py pdfminer\cmap Adobe-GB1 cmaprsrc\cid2code_Adobe_GB1.txt cp936 gb2312 python tools\conv_cmap.py pdfminer\cmap Adobe-Japan1 cmaprsrc\cid2code_Adobe_Japan1.txt cp932 euc-jp python tools\conv_cmap.py pdfminer\cmap Adobe-Korea1 cmaprsrc\cid2code_Adobe_Korea1.txt cp949 euc-kr python setup.py install
PDFMiner comes with two handy tools:
pdf2txt.py
and dumppdf.py
.
pdf2txt.py
extracts text contents from a PDF file.
It extracts all the texts that are to be rendered programmatically,
ie. text represented as ASCII or Unicode strings.
It cannot recognize texts drawn as images that would require optical character recognition.
It also extracts the corresponding locations, font names, font sizes, writing
direction (horizontal or vertical) for each text portion.
You need to provide a password for protected PDF documents when its access is restricted.
You cannot extract any text from a PDF document which does not have extraction permission.
Note: Not all characters in a PDF can be safely converted to Unicode.
$ pdf2txt.py -o output.html samples/naacl06-shinyama.pdf (extract text as an HTML file whose filename is output.html) $ pdf2txt.py -c euc-jp -o output.html samples/jo.pdf (extract a Japanese HTML file in vertical writing, CMap is required) $ pdf2txt.py -P mypassword -o output.txt secret.pdf (extract a text from an encrypted PDF file)
-o filename
-p pageno[,pageno,...]
-c codec
-t type
text
: TEXT format. (Default)
html
: HTML format. Not recommended for extraction purposes because the markup is messy.
xml
: XML format. Provides the most information available.
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").
-I image_directory
-M char_margin
-L line_margin
-W word_margin
Each value is specified not as an actual length, but as a proportion of the length to the size of each character in question. The default values are M = 1.0, L = 0.3, and W = 0.2, respectively.
→ | ← M | ||||||
Q u i |
c k |
b r o w |
n f o x |
↓ | |||
→ | ← W | L | |||||
|
↑ |
-n
-A
-Y layout_mode
exact
: preserve the exact location of each individual character (a large and messy HTML).
normal
: preserve the location and line breaks in each text block. (Default)
loose
: preserve the overall location of each text block.
-s scale
-m maxpages
-P password
-d
dumppdf.py
dumps the internal contents of a PDF file
in pseudo-XML format. This program is primarily for debugging purposes,
but it's also possible to extract some meaningful contents
(such as images).
$ dumppdf.py -a foo.pdf (dump all the headers and contents, except stream objects) $ dumppdf.py -T foo.pdf (dump the table of contents) $ dumppdf.py -r -i6 foo.pdf > pic.jpeg (extract a JPEG image)
-a
-i objno,objno, ...
-i
options are accepted.
-p pageno,pageno, ...
-p
options are accepted.
Note that page numbers start from one, not zero.
-r
(raw)
-b
(binary)
-t
(text)
With -r
option, the "raw" stream contents are dumped without decompression.
With -b
option, the decompressed contents are dumped as a binary blob.
With -t
option, the decompressed 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.
-T
-P password
-d
(This is so-called MIT/X License)
Copyright (c) 2004-2010 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.