invoice-extraction / utils.py
Sakshi
created invoice extraction app
ddacfa7
Raw
History Blame Contribute Delete
1.25 kB
"""
Utilities
@author : Sakshi Tantak
"""
import streamlit as st
import base64
def markdown_table_to_json(markdown):
lines = markdown.strip().split("\n")
# Extract headers
headers = [h.strip() for h in lines[0].split("|") if h.strip()]
# Extract rows
rows = []
for line in lines[2:]: # Skip header and separator line
values = [v.strip() for v in line.split("|") if v.strip()]
row_dict = dict(zip(headers, values))
rows.append(row_dict)
return rows
def validate_pdf(pdf_bytes: bytes) -> bool:
"""
Validates the uploaded PDF file.
"""
if not pdf_bytes:
return False
# Check file signature for PDF (%PDF-)
return pdf_bytes.startswith(b'%PDF-')
def displayPDF(file):
# Opening file from file path
if isinstance(file, str):
file_bytes = open(file, 'rb').read()
else:
file_bytes = file
# with open(file, "rb") as f:
base64_pdf = base64.b64encode(file_bytes).decode('utf-8')
# Embedding PDF in HTML
pdf_display = F'<embed src="data:application/pdf;base64,{base64_pdf}" width="700" height="1000" type="application/pdf">'
# Displaying File
st.markdown(pdf_display, unsafe_allow_html=True)