File: extract_pages.rst

package info (click to toggle)
pdfminer 20200726-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 15,716 kB
  • sloc: python: 13,377; xml: 423; makefile: 95; sh: 3
file content (47 lines) | stat: -rw-r--r-- 1,690 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
.. _tutorial_extract_pages:

Extract elements from a PDF using Python
****************************************

The high level functions can be used to achieve common tasks. In this case,
we can use :ref:`api_extract_pages`:

.. code-block:: python

   from pdfminer.high_level import extract_pages
   for page_layout in extract_pages("test.pdf"):
       for element in page_layout:
           print(element)


Each ``element`` will be an ``LTTextBox``, ``LTFigure``, ``LTLine``, ``LTRect``
or an ``LTImage``. Some of these can be iterated further, for example iterating
though an ``LTTextBox`` will give you an ``LTTextLine``, and these in turn can
be iterated through to get an ``LTChar``. See the diagram here:
:ref:`topic_pdf_to_text_layout`.

Let's say we want to extract all of the text. We could do:

.. code-block:: python

   from pdfminer.high_level import extract_pages
   from pdfminer.layout import LTTextContainer
   for page_layout in extract_pages("test.pdf"):
       for element in page_layout:
           if isinstance(element, LTTextContainer):
               print(element.get_text())

Or, we could extract the fontname or size of each individual character:

.. code-block:: python

   from pdfminer.high_level import extract_pages
   from pdfminer.layout import LTTextContainer, LTChar
   for page_layout in extract_pages("test.pdf"):
       for element in page_layout:
           if isinstance(element, LTTextContainer):
               for text_line in element:
                   for character in text_line:
                       if isinstance(character, LTChar):
                           print(character.fontname)
                           print(character.size)