Skip to content
Snippets Groups Projects
Commit dfd53745 authored by larsm's avatar larsm
Browse files

refactoring

contents werden jetzt als liste in die shmebulok geschrieben
parent cdb28bac
No related branches found
No related tags found
No related merge requests found
......@@ -11,7 +11,7 @@ from pdfminer.converter import PDFPageAggregator
from pdfminer.layout import LTTextBoxHorizontal
def get_text_elements(p_path):
def get_textelements_from_pdf(p_path):
"""
Gets all text-elements of the given document as list.
"""
......@@ -114,10 +114,11 @@ def is_point_in_box(p_point, p_box, p_padding=5):
return x0 <= x and x <= x1 and y0 <= y and y <= y1
def read_template(p_template, p_text_element_list):
def get_contents_from_textelements(p_template, p_text_element_list):
"""
Analysis all text elements according to the given template.
"""
text_values = []
for key, pos in p_template.items():
key_box = None
for element in p_text_element_list:
......@@ -138,6 +139,8 @@ def read_template(p_template, p_text_element_list):
for i in range(max_padding):
if is_point_in_box(estimated_position, element.bbox, i):
text = element.get_text().replace("\n", " ").split(" ")
text_values.append({key: text[offset]})
print("Found value %s: '%s' with offset %d" % (key, text[offset], i))
break
#print("Found no value for '%s' with max padding %d" % (key, max_padding))
return text_values
from pdf_parser import get_text_elements, generate_template, read_template
from pdf_parser import get_textelements_from_pdf, generate_template, get_contents_from_textelements
import json
......@@ -7,7 +7,7 @@ def create_template(p_name, p_path, p_examples):
Generates a template from example and pdf file and
stores it as json
"""
elements = get_text_elements(p_path)
elements = get_textelements_from_pdf(p_path)
template = generate_template(p_examples, elements)
data = [p_examples, template]
......@@ -20,12 +20,12 @@ def evaluate_pdf(p_template_name, p_path):
Analysis a pdf according to the given template
"""
print('\nEvaluating:', p_path)
with open(p_template_name + '.template') as f:
data = json.load(f)
template = data[1]
elements = get_text_elements(p_path)
content = read_template(template, elements)
data = json.load(f) # [{'Rechnung': 'EB589544', 'Rechnungsdatum': '01.09.2016', 'Mandatsreferenz': '8ADB836C18A8491092EA62F7F35F8A28'}, {'Rechnung': [[125.29500000000002, 29.985000000000014], 2], 'Rechnungsdatum': [[125.29500000000002, 29.985000000000014], 0], 'Mandatsreferenz': [[125.90500000000006, 6.095000000000027], 1]}]
template = data[1] # {'Rechnung': [[125.29500000000002, 29.985000000000014], 2], 'Rechnungsdatum': [[125.29500000000002, 29.985000000000014], 0], 'Mandatsreferenz': [[125.90500000000006, 6.095000000000027], 1]}
textelements = get_textelements_from_pdf(p_path) # [<LTTextBoxHorizontal(0) 60.000,712.080,231.864,721.376 'sipgate GmbH - Gladbacher Str. 74 - 40219 Düsseldorf\n'>, <LTTextBoxHorizontal(1) 65.000,663.580,163.690,699.200 'Warpzone e.V.\nJan-Marten Brüggemann\nAm Hawerkamp 31\n'>, <LTTextBoxHorizontal(2) 65.000,639.470,129.350,651.660 '48155 Münster\n'>, <LTTextBoxHorizontal(3) 375.000,663.580,451.560,723.200 'Rechnungsdatum\nLeistungsdatum\nRechnungsnummer\nBezahlung per\nKundennummer\n'>, <LTTextBoxHorizontal(4) 465.000,663.470,535.590,723.660 '01.09.2016\n01.09.2016\nEB589544\nSEPA-Lastschrift\n1967957\n'>, <LTTextBoxHorizontal(5) 375.000,617.500,395.280,629.120 'Seite\n'>, <LTTextBoxHorizontal(6) 465.000,617.390,485.230,629.580 '1 / 1\n'>, <LTTextBoxHorizontal(7) 60.000,555.824,200.976,575.328 'Rechnung EB589544\n'>, <LTTextBoxHorizontal(8) 90.000,537.390,184.160,549.580 'Art.-Nr. Bezeichnung\n'>, <LTTextBoxHorizontal(9) 60.000,525.500,78.050,549.580 'Pos.\n \n'>, <LTTextBoxHorizontal(10) 280.000,537.390,307.500,549.580 'Menge\n'>, <LTTextBoxHorizontal(11) 318.000,525.500,364.840,549.580 'Einzelpreis\nnetto\n'>, <LTTextBoxHorizontal(12) 378.000,525.500,424.840,549.580 'Einzelpreis\nbrutto\n'>, <LTTextBoxHorizontal(13) 438.000,525.500,452.930,549.580 'USt\n \n'>, <LTTextBoxHorizontal(14) 468.000,525.500,520.580,549.580 'Gesamtpreis\nnetto\n'>, <LTTextBoxHorizontal(15) 60.000,489.500,67.330,501.120 ' 1\n'>, <LTTextBoxHorizontal(16) 90.000,489.500,95.000,501.120 '1\n'>, <LTTextBoxHorizontal(17) 130.000,489.500,252.050,501.120 'sipgate.de, Telefonieguthaben\n'>, <LTTextBoxHorizontal(18) 302.440,489.500,307.440,501.120 '1\n'>, <LTTextBoxHorizontal(19) 348.360,489.500,365.250,501.120 '8,40\n'>, <LTTextBoxHorizontal(20) 402.800,489.500,424.650,501.120 '10,00\n'>, <LTTextBoxHorizontal(21) 438.000,489.500,455.590,501.120 '19%\n'>, <LTTextBoxHorizontal(22) 498.950,489.500,534.210,501.120 '8,40 EUR\n'>, <LTTextBoxHorizontal(23) 310.000,447.500,408.610,459.120 'Summe Positionen netto\n'>, <LTTextBoxHorizontal(24) 310.000,411.500,419.970,423.120 '19% USt. auf EUR 8,40 (DE)\n'>, <LTTextBoxHorizontal(25) 492.740,447.390,531.790,459.580 '8,40 EUR\n'>, <LTTextBoxHorizontal(26) 492.780,411.390,531.830,423.580 '1,60 EUR\n'>, <LTTextBoxHorizontal(27) 310.000,369.390,383.390,381.580 'Rechnungsbetrag\n'>, <LTTextBoxHorizontal(28) 487.780,369.390,532.390,381.580 '10,00 EUR\n'>, <LTTextBoxHorizontal(29) 60.000,307.390,414.900,331.580 'Der Betrag in Höhe von 10,00 EUR wird am 06.09.2016 von Ihrem Konto abgebucht.\nInhaber: WARPZONE E.V., IBAN: DE70XXXXXXXXXXXXXXXXX3738, BIC: WELADED1MST\n'>, <LTTextBoxHorizontal(30) 60.000,283.390,311.810,295.580 'Mandatsreferenz: 8ADB836C18A8491092EA62F7F35F8A28\n'>, <LTTextBoxHorizontal(31) 60.000,223.500,485.630,271.120 'Die Umsatzsteuer wird in Höhe des in Ihrem Land geltenden Umsatzsteuersatzes in Rechnung gestellt. Der\nUmsatzsteuersatz bestimmt sich nach dem Ort der Leistung, d.h. nach dem Sitz Ihres Unternehmens bzw.\nnach Ihrem gewöhnlichen Aufenthaltsort. Sollten Ihre Rechnung (Anschrift bzw. Umsatzsteuersatz) nicht\nkorrekt sein, informieren Sie bitte unsere Kundenbetreuung unter basic@sipgate.de.\n'>, <LTTextBoxHorizontal(32) 50.000,58.000,377.192,77.296 'sipgate GmbH, Gladbacher Str. 74, 40219 Düsseldorf, HRB 39841 Düsseldorf, GF: Tim Mois, Thilo Salmon\nUSt-ID: DE219349391, Finanzamt Düsseldorf, Steuer-Nr.: 106/5724/7147, Support: basic@sipgate.de\n'>, <LTTextBoxHorizontal(33) 50.000,28.000,334.008,47.296 'Bank: Commerzbank Düsseldorf, IBAN: DE10 3004 0000 0181 1488 06, BIC: COBADEFFXXX\nGläubiger-ID: DE73ZZZ00000359204\n'>]
contents = get_contents_from_textelements(template, textelements) # None
with open("output.shmebulok", "w") as out:
json.dump(content, out)
json.dump(contents, out)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment