2024-11-04 15:57:21 +01:00
|
|
|
import re
|
|
|
|
from pdfminer.high_level import extract_text
|
2024-10-31 12:46:23 +01:00
|
|
|
|
2024-11-04 15:57:21 +01:00
|
|
|
|
|
|
|
my_path = r"C:\Users\DELL\Downloads\Kopia 1_240014-333-361-389-399-433-441-448-464-481_2024_06_20_CE240143 (INVOICE - ERD-FVZ%i9210621%i922024%i92NZ-1 - 1 - A1) - 1.pdf.pdf"
|
|
|
|
#my_path = r"C:/Users/DELL/Downloads/A-24VU-00511.PDF.pdf"
|
|
|
|
|
|
|
|
|
|
|
|
#regex
|
2024-11-03 12:15:47 +01:00
|
|
|
postal_code_pattern = r"(?<!\S)(?:(?:[A-Za-z]{3,}\s+)(\d{5}|\d{2}-\d{3})|(\d{5}|\d{2}-\d{3})(?:\s+[A-Za-z]{3,}))(?!\S)"
|
2024-11-04 15:57:21 +01:00
|
|
|
postal_code_pattern2 = r"(?<!\S)(?:[A-Za-z]{3,}\s+-?\s+(\d{5}|\d{2}-\d{3})|(\d{5}|\d{2}-\d{3})\s+-?\s+[A-Za-z]{3,})(?!\S)"
|
|
|
|
date_pattern = r'\b\d{2}/\d{2}/\d{2,4}\b'
|
|
|
|
reference_pattern = r"ref\.\s*[^0-9]*?([1-9]\d{4,})"
|
|
|
|
|
|
|
|
order_numbers = []
|
|
|
|
order_dates = set()
|
|
|
|
buyers = []
|
|
|
|
|
|
|
|
text = extract_text(my_path).lower()
|
|
|
|
|
|
|
|
def dates(text):
|
|
|
|
matches = re.findall(date_pattern, text)
|
|
|
|
for match in matches:
|
|
|
|
order_dates.add(match)
|
|
|
|
|
|
|
|
def ref_numbers(text):
|
|
|
|
matches = re.findall(reference_pattern, text)
|
|
|
|
for match in matches:
|
|
|
|
order_numbers.append(match)
|
|
|
|
|
|
|
|
|
|
|
|
def company(text):
|
|
|
|
lines = text.splitlines()
|
|
|
|
for i in range(len(lines)):
|
|
|
|
line = lines[i]
|
|
|
|
|
|
|
|
# Sprawdzenie, czy linia zawiera kod pocztowy
|
|
|
|
if re.search(postal_code_pattern, line) or re.search(postal_code_pattern2, line):
|
|
|
|
# Szukanie linii powyżej
|
|
|
|
line_above = next((lines[j].strip() for j in range(i - 1, -1, -1) if len(lines[j].strip()) > 0), '')
|
|
|
|
# Szukanie linii z nazwą firmy
|
|
|
|
line_2 = next((lines[j].strip() for j in range(i - 1, -1, -1) if len(lines[j].strip()) > 0 and 'ul.' not in lines[j].strip() and lines[j].strip() != line_above), '')
|
|
|
|
# Szukanie linii poniżej
|
|
|
|
line_below = next((lines[j].strip() for j in range(i + 1, len(lines)) if len(lines[j].strip()) > 0), '')
|
|
|
|
|
|
|
|
if line_2 and line_above and line and line_below:
|
|
|
|
buyer = {
|
2024-11-03 12:15:47 +01:00
|
|
|
'line_2': line_2,
|
|
|
|
'line_above': line_above,
|
|
|
|
'postal_code_line': line,
|
|
|
|
'line_below': line_below
|
2024-11-04 15:57:21 +01:00
|
|
|
}
|
|
|
|
if buyer not in buyers:
|
|
|
|
buyers.append(buyer)
|
|
|
|
|
|
|
|
dates(text)
|
|
|
|
ref_numbers(text)
|
|
|
|
company(text)
|
|
|
|
|
|
|
|
order_dates = list(order_dates)
|
2024-10-31 12:46:23 +01:00
|
|
|
|
|
|
|
print("Reference numbers:", order_numbers)
|
|
|
|
print("Document dates:", order_dates)
|
|
|
|
print("Buyers: ")
|
2024-11-04 15:57:21 +01:00
|
|
|
|
2024-10-31 12:46:23 +01:00
|
|
|
for context in buyers:
|
2024-11-04 15:57:21 +01:00
|
|
|
print("Company:", context['line_2'])
|
|
|
|
print("St.:", context['line_above'])
|
|
|
|
print("City:", context['postal_code_line'])
|
2024-10-31 12:46:23 +01:00
|
|
|
print("Country:", context['line_below'])
|
|
|
|
print()
|