Module: Tabula

Defined in:
lib/tabula.rb,
lib/tabula/cli.rb,
lib/tabula/version.rb,
lib/tabula/pdf/page.rb,
lib/tabula/text/line.rb,
lib/tabula/core/point.rb,
lib/tabula/table/cell.rb,
lib/tabula/core/ruling.rb,
lib/tabula/table/table.rb,
lib/tabula/configuration.rb,
lib/tabula/core/rectangle.rb,
lib/tabula/writers/writer.rb,
lib/tabula/text/text_chunk.rb,
lib/tabula/pdf/text_stripper.rb,
lib/tabula/text/text_element.rb,
lib/tabula/core/spatial_index.rb,
lib/tabula/writers/csv_writer.rb,
lib/tabula/writers/tsv_writer.rb,
lib/tabula/writers/json_writer.rb,
lib/tabula/pdf/object_extractor.rb,
lib/tabula/writers/markdown_writer.rb,
lib/tabula/algorithms/projection_profile.rb,
lib/tabula/detectors/detection_algorithm.rb,
lib/tabula/extractors/extraction_algorithm.rb,
lib/tabula/algorithms/cohen_sutherland_clipping.rb,
lib/tabula/extractors/basic_extraction_algorithm.rb,
lib/tabula/detectors/nurminen_detection_algorithm.rb,
lib/tabula/detectors/spreadsheet_detection_algorithm.rb,
lib/tabula/extractors/spreadsheet_extraction_algorithm.rb

Defined Under Namespace

Modules: CohenSutherlandClipping, Detectors, Extractors, Writers Classes: CLI, Cell, Configuration, Error, FileNotFoundError, InvalidOptionsError, InvalidPDFError, Line, ObjectExtractor, Page, PageIterator, PasswordRequiredError, Point, ProjectionProfile, Rectangle, Ruling, SpatialIndex, Table, TextChunk, TextElement, TextStripper

Constant Summary collapse

VALID_METHODS =
%i[lattice stream auto].freeze
VERSION =
'1.0.3'

Class Method Summary collapse

Class Method Details

.configuration ⇒ Configuration

Get the default configuration

Returns:



103
104
105
# File 'lib/tabula/configuration.rb', line 103

def configuration
  @default_configuration
end

.configuration=(config) ⇒ Object

Set the default configuration

Parameters:



109
110
111
# File 'lib/tabula/configuration.rb', line 109

def configuration=(config)
  @default_configuration = config
end

.configure {|Configuration| ... } ⇒ Object

Configure with a block

Yields:



115
116
117
# File 'lib/tabula/configuration.rb', line 115

def configure
  yield configuration
end

.extract(path, **options) ⇒ Array<Table>

Extract tables from a PDF file

Parameters:

  • path (String) —

    path to PDF file

  • options (Hash) —

    extraction options

Options Hash (**options):

  • :pages (Array<Integer>) —

    pages to extract (1-indexed, nil for all)

  • :method (Symbol) —

    extraction method (:lattice, :stream, or :auto)

  • :area (Array<Float>) —

    area to extract [top, left, bottom, right]

  • :columns (Array<Float>) —

    column boundaries

  • :password (String) —

    PDF password

  • :guess (Boolean) —

    auto-detect table areas

Returns:

  • (Array<Table>) —

    extracted tables

Raises:



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/tabula.rb', line 70

def extract(path, **options)
  validate_file!(path)
  validate_options!(options)

  ObjectExtractor.open(path, password: options[:password]) do |extractor|
    pages = options[:pages] || (1..extractor.page_count).to_a
    method = options[:method] || :auto
    area = options[:area]
    columns = options[:columns]
    guess = options.fetch(:guess, false)

    tables = []

    pages.each do |page_num|
      page = extractor.extract_page(page_num)
      page = page.get_area(*area) if area

      if guess
        detected_areas = Detectors::Nurminen.detect(page)
        detected_areas.each do |detected_area|
          sub_page = page.get_area(*detected_area.bounds)
          tables.concat(extract_from_page(sub_page, method, columns))
        end
      else
        tables.concat(extract_from_page(page, method, columns))
      end
    end

    tables
  end
end