Create parse_federal_transformers.py
Browse files
parse_federal_transformers.py
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from typing import Dict
|
| 3 |
+
|
| 4 |
+
def parse_federal_transformers(file_text: str) -> Dict:
|
| 5 |
+
parsed_data = {
|
| 6 |
+
"Purchase Order No": "",
|
| 7 |
+
"Date": "",
|
| 8 |
+
"Supplier": "Federal Transformers Co. LLC",
|
| 9 |
+
"Invoice Address": "",
|
| 10 |
+
"Delivery Address": "",
|
| 11 |
+
"Currency": "",
|
| 12 |
+
"Payment Terms": "",
|
| 13 |
+
"Items": []
|
| 14 |
+
}
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
parsed_data["Purchase Order No"] = re.search(r"Purchase Order No\.\s(\d+)", file_text).group(1)
|
| 18 |
+
parsed_data["Date"] = re.search(r"Date:\s+(\d{2}-\w{3}-\d{2})", file_text).group(1)
|
| 19 |
+
|
| 20 |
+
parsed_data["Invoice Address"] = re.search(r"Invoice Address\s*:\s*(.*?)(?=\sDelivery Address)", file_text, re.DOTALL).group(1).strip()
|
| 21 |
+
parsed_data["Delivery Address"] = re.search(r"Delivery Address\s*:\s*(.*?)(?=\sNote)", file_text, re.DOTALL).group(1).strip()
|
| 22 |
+
|
| 23 |
+
item_pattern = re.compile(r"(\d+)\s+([\w\s]+)\s+(\d+)\s+([\d.]+)\s+([\d.]+)")
|
| 24 |
+
for match in item_pattern.finditer(file_text):
|
| 25 |
+
parsed_data["Items"].append({
|
| 26 |
+
"Item No": match.group(1),
|
| 27 |
+
"Description": match.group(2).strip(),
|
| 28 |
+
"Quantity": int(match.group(3)),
|
| 29 |
+
"Unit Price": float(match.group(4)),
|
| 30 |
+
"Total Price": float(match.group(5)),
|
| 31 |
+
})
|
| 32 |
+
except Exception as e:
|
| 33 |
+
print(f"Error parsing Federal Transformers PO: {e}")
|
| 34 |
+
|
| 35 |
+
return parsed_data
|