structured data extraction from pdf

This commit is contained in:
2026-01-22 21:08:50 +05:30
parent d662aea7e4
commit 9d7109b60f
4 changed files with 34 additions and 14 deletions

View File

@@ -10,8 +10,39 @@ from pypdf import PdfReader
import pytesseract
from PIL import Image
from pdf2image import convert_from_bytes
import pdfplumber
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
from database import get_db, Email
from scheduler import start_scheduler, stop_scheduler
@@ -57,19 +88,7 @@ class LoginResponse(BaseModel):
class NERResponse(BaseModel):
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:
try:

View File

@@ -9,3 +9,4 @@ psycopg2-binary
imap-tools
apscheduler
python-dotenv
pdfplumber