Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Area Plugin Support #305

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/invoice2data/extract/invoice_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from unidecode import unidecode
import logging
from collections import OrderedDict
from .plugins import lines, tables
from .plugins import lines, tables,area

logger = logging.getLogger(__name__)

Expand All @@ -24,7 +24,7 @@
"replace": [], # example: see templates/fr/fr.free.mobile.yml
}

PLUGIN_MAPPING = {"lines": lines, "tables": tables}
PLUGIN_MAPPING = {"lines": lines, "tables": tables, 'area':area}


class InvoiceTemplate(OrderedDict):
Expand Down Expand Up @@ -130,7 +130,7 @@ def coerce_type(self, value, target_type):
return self.parse_date(value)
assert False, "Unknown type"

def extract(self, optimized_str):
def extract(self, optimized_str,path):
"""
Given a template file and a string, extract matching data fields.
"""
Expand Down Expand Up @@ -206,7 +206,7 @@ def extract(self, optimized_str):
# Run plugins:
for plugin_keyword, plugin_func in PLUGIN_MAPPING.items():
if plugin_keyword in self.keys():
plugin_func.extract(self, optimized_str, output)
plugin_func.extract(self, optimized_str, path, output)

# If required fields were found, return output, else log error.
if "required_fields" not in self.keys():
Expand Down
57 changes: 57 additions & 0 deletions src/invoice2data/extract/plugins/area.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
"""
Plugin to extract area from an invoice.
"""

import logging
import re
import subprocess
from distutils import spawn # py2 compat

logger = logging.getLogger(__name__)

DEFAULT_OPTIONS = {'field_separator': r'\s+', 'line_separator': r'\n'}


def extract(self, content, path, output):
"""Try to extract values using area from an invoice"""

for area in self['area']:

# First apply default options.
plugin_settings = DEFAULT_OPTIONS.copy()
plugin_settings.update(area)
area = plugin_settings

# Validate settings
assert 'name' in area, 'Area name missing'
assert 'area' in area, 'Area area details missing'
assert 'r' in area["area"], 'Area R details missing'
assert 'x' in area["area"], 'Area X details missing'
assert 'y' in area["area"], 'Area y details missing'
assert 'W' in area["area"], 'Area W details missing'
assert 'H' in area["area"], 'Area H details missing'
r = str(area['area']["r"])
x = str(area['area']["x"])
y = str(area['area']["y"])
W = str(area['area']["W"])
H = str(area['area']["H"])
if spawn.find_executable("pdftotext"): # shutil.which('pdftotext'):
out, err = subprocess.Popen(
["pdftotext","-layout", "-enc", "UTF-8",'-r',r,'-x',x,'-y',y,'-W',W,'-H',H, path, '-'],
stdout=subprocess.PIPE


).communicate()
if 'regex' in area:
reg_string = re.search(area["regex"], out.decode("utf-8"))
output[area["name"]] = reg_string.group()
else:
output[area["name"]] = out.decode("utf-8")
else:
raise EnvironmentError(
'pdftotext not installed. Can be downloaded from https://poppler.freedesktop.org/'
)



# logger.debug('ignoring *%s* because it doesn\'t match anything', line)
2 changes: 1 addition & 1 deletion src/invoice2data/extract/plugins/lines.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
DEFAULT_OPTIONS = {"field_separator": r"\s+", "line_separator": r"\n"}


def extract(self, content, output):
def extract(self, content, path, output):
"""Try to extract lines from the invoice"""

# First apply default options.
Expand Down
2 changes: 1 addition & 1 deletion src/invoice2data/extract/plugins/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
DEFAULT_OPTIONS = {"field_separator": r"\s+", "line_separator": r"\n"}


def extract(self, content, output):
def extract(self, content, path, output):
"""Try to extract tables from an invoice"""

for table in self["tables"]:
Expand Down
2 changes: 1 addition & 1 deletion src/invoice2data/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def extract_data(invoicefile, templates=None, input_module=pdftotext):
optimized_str = t.prepare_input(extracted_str)

if t.matches_input(optimized_str):
return t.extract(optimized_str)
return t.extract(optimized_str,invoicefile)

logger.error("No template for %s", invoicefile)
return False
Expand Down