structured data extraction from pdf
This commit is contained in:
Binary file not shown.
@@ -10,8 +10,39 @@ from pypdf import PdfReader
|
|||||||
import pytesseract
|
import pytesseract
|
||||||
from PIL import Image
|
from PIL import Image
|
||||||
from pdf2image import convert_from_bytes
|
from pdf2image import convert_from_bytes
|
||||||
|
import pdfplumber
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# 2. OCR Module
|
||||||
|
def extract_text_from_pdf(file_bytes: bytes) -> str:
|
||||||
|
# Try pdfplumber first for layout preservation
|
||||||
|
try:
|
||||||
|
with pdfplumber.open(io.BytesIO(file_bytes)) as pdf:
|
||||||
|
text = ""
|
||||||
|
for page in pdf.pages:
|
||||||
|
page_text = page.extract_text(layout=True)
|
||||||
|
if page_text:
|
||||||
|
text += page_text + "\n"
|
||||||
|
if text.strip():
|
||||||
|
return text.strip()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"pdfplumber error: {e}")
|
||||||
|
|
||||||
|
# Fallback to pypdf
|
||||||
|
try:
|
||||||
|
reader = PdfReader(io.BytesIO(file_bytes))
|
||||||
|
text = ""
|
||||||
|
for page in reader.pages:
|
||||||
|
page_text = page.extract_text()
|
||||||
|
if page_text:
|
||||||
|
text += page_text + "\n"
|
||||||
|
return text.strip()
|
||||||
|
except Exception as e:
|
||||||
|
print(f"Error reading PDF: {e}")
|
||||||
|
return ""
|
||||||
|
|
||||||
# Internal modules
|
# Internal modules
|
||||||
from database import get_db, Email
|
from database import get_db, Email
|
||||||
from scheduler import start_scheduler, stop_scheduler
|
from scheduler import start_scheduler, stop_scheduler
|
||||||
@@ -57,19 +88,7 @@ class LoginResponse(BaseModel):
|
|||||||
class NERResponse(BaseModel):
|
class NERResponse(BaseModel):
|
||||||
text: str
|
text: str
|
||||||
|
|
||||||
# 2. OCR Module
|
|
||||||
def extract_text_from_pdf(file_bytes: bytes) -> str:
|
|
||||||
try:
|
|
||||||
reader = PdfReader(io.BytesIO(file_bytes))
|
|
||||||
text = ""
|
|
||||||
for page in reader.pages:
|
|
||||||
page_text = page.extract_text()
|
|
||||||
if page_text:
|
|
||||||
text += page_text + "\n"
|
|
||||||
return text.strip()
|
|
||||||
except Exception as e:
|
|
||||||
print(f"Error reading PDF: {e}")
|
|
||||||
return ""
|
|
||||||
|
|
||||||
def extract_text_from_image(file_bytes: bytes) -> str:
|
def extract_text_from_image(file_bytes: bytes) -> str:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -9,3 +9,4 @@ psycopg2-binary
|
|||||||
imap-tools
|
imap-tools
|
||||||
apscheduler
|
apscheduler
|
||||||
python-dotenv
|
python-dotenv
|
||||||
|
pdfplumber
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ import { ToastModule } from 'primeng/toast';
|
|||||||
[(ngModel)]="extractedText"
|
[(ngModel)]="extractedText"
|
||||||
readonly
|
readonly
|
||||||
class="w-full"
|
class="w-full"
|
||||||
style="min-height: 300px; width: 100%; border-color: #d1d5db;">
|
style="min-height: 300px; width: 100%; border-color: #d1d5db; font-family: monospace;">
|
||||||
</textarea>
|
</textarea>
|
||||||
</div>
|
</div>
|
||||||
<p-toast></p-toast>
|
<p-toast></p-toast>
|
||||||
|
|||||||
Reference in New Issue
Block a user