Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,129 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import fitz # PyMuPDF for PDF handling
|
3 |
+
import easyocr # OCR for text extraction
|
4 |
+
import tempfile
|
5 |
+
import streamlit as st
|
6 |
+
|
7 |
+
|
8 |
+
def extract_text_with_positions_using_ocr(pdf_path):
|
9 |
+
"""
|
10 |
+
Extract text with bounding box positions using OCR for both English and Arabic text.
|
11 |
+
:param pdf_path: Path to the input PDF file.
|
12 |
+
:return: List of dictionaries containing text and positions for each page.
|
13 |
+
"""
|
14 |
+
extracted_data = []
|
15 |
+
doc = fitz.open(pdf_path)
|
16 |
+
|
17 |
+
# Convert each PDF page to an image for OCR processing
|
18 |
+
for page_num in range(len(doc)):
|
19 |
+
page = doc.load_page(page_num)
|
20 |
+
pix = page.get_pixmap(dpi=300) # Convert PDF page to image
|
21 |
+
image_path = f"temp_page_{page_num}.png"
|
22 |
+
pix.save(image_path)
|
23 |
+
|
24 |
+
# Perform OCR on the image
|
25 |
+
reader = easyocr.Reader(['en', 'ar']) # Supports English and Arabic
|
26 |
+
results = reader.readtext(image_path, detail=1) # detail=1 returns bounding box info
|
27 |
+
|
28 |
+
# Extract text and positions
|
29 |
+
page_data = []
|
30 |
+
for (bbox, text, confidence) in results:
|
31 |
+
(x0, y0), (x1, y1) = bbox[0], bbox[2]
|
32 |
+
page_data.append({
|
33 |
+
"text": text,
|
34 |
+
"x0": x0,
|
35 |
+
"y0": y1, # Adjust to bottom-left corner (PDF coordinates)
|
36 |
+
"x1": x1,
|
37 |
+
"y1": y0,
|
38 |
+
"font_size": y1 - y0 # Approximate font size
|
39 |
+
})
|
40 |
+
|
41 |
+
extracted_data.append(page_data)
|
42 |
+
|
43 |
+
# Cleanup temporary image
|
44 |
+
os.remove(image_path)
|
45 |
+
|
46 |
+
return extracted_data
|
47 |
+
|
48 |
+
|
49 |
+
def overlay_text_with_fonts(pdf_path, extracted_data, output_pdf_path):
|
50 |
+
"""
|
51 |
+
Overlay extracted text onto the original PDF using fonts from different font families.
|
52 |
+
:param pdf_path: Path to the input PDF file.
|
53 |
+
:param extracted_data: List of extracted text with positions.
|
54 |
+
:param output_pdf_path: Path to save the output PDF file.
|
55 |
+
"""
|
56 |
+
doc = fitz.open(pdf_path)
|
57 |
+
|
58 |
+
# Define font families for overlay
|
59 |
+
font_families = ["Times-Roman", "Helvetica", "Courier"] # Example font families
|
60 |
+
|
61 |
+
for page_num, page_data in enumerate(extracted_data):
|
62 |
+
page = doc[page_num]
|
63 |
+
|
64 |
+
for item in page_data:
|
65 |
+
font = font_families[page_num % len(font_families)] # Rotate fonts across pages
|
66 |
+
page.insert_text(
|
67 |
+
(item["x0"], item["y0"]),
|
68 |
+
item["text"],
|
69 |
+
fontsize=item["font_size"],
|
70 |
+
fontname=font,
|
71 |
+
color=(0, 0, 0), # Black text
|
72 |
+
render_mode=0 # Ensure text is not outlined
|
73 |
+
)
|
74 |
+
|
75 |
+
doc.save(output_pdf_path)
|
76 |
+
print(f"PDF saved to: {output_pdf_path}")
|
77 |
+
|
78 |
+
|
79 |
+
def process_pdf(uploaded_pdf, output_pdf_path):
|
80 |
+
"""
|
81 |
+
Process the uploaded PDF to extract text using OCR and overlay it as editable text.
|
82 |
+
:param uploaded_pdf: The uploaded PDF file.
|
83 |
+
:param output_pdf_path: Path to save the output PDF file.
|
84 |
+
"""
|
85 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as temp_pdf:
|
86 |
+
temp_pdf.write(uploaded_pdf.read())
|
87 |
+
temp_pdf_path = temp_pdf.name
|
88 |
+
|
89 |
+
# Step 1: Extract text using OCR
|
90 |
+
extracted_data = extract_text_with_positions_using_ocr(temp_pdf_path)
|
91 |
+
|
92 |
+
# Step 2: Overlay extracted text onto the original PDF
|
93 |
+
overlay_text_with_fonts(temp_pdf_path, extracted_data, output_pdf_path)
|
94 |
+
|
95 |
+
# Cleanup temporary file
|
96 |
+
if os.path.exists(temp_pdf_path):
|
97 |
+
os.remove(temp_pdf_path)
|
98 |
+
|
99 |
+
|
100 |
+
# Streamlit App
|
101 |
+
def main():
|
102 |
+
st.title("PDF Text OCR and Overlay Tool")
|
103 |
+
st.write("Upload a PDF to extract and overlay text as editable layers with different fonts.")
|
104 |
+
|
105 |
+
uploaded_file = st.file_uploader("Upload PDF", type=["pdf"])
|
106 |
+
if uploaded_file:
|
107 |
+
output_pdf_path = "corrected_output_with_fonts.pdf"
|
108 |
+
|
109 |
+
with st.spinner("Processing your PDF..."):
|
110 |
+
process_pdf(uploaded_file, output_pdf_path)
|
111 |
+
|
112 |
+
st.success("PDF processing complete!")
|
113 |
+
|
114 |
+
# Provide a download button for the processed PDF
|
115 |
+
with open(output_pdf_path, "rb") as f:
|
116 |
+
st.download_button(
|
117 |
+
label="Download Corrected PDF",
|
118 |
+
data=f,
|
119 |
+
file_name="corrected_output_with_fonts.pdf",
|
120 |
+
mime="application/pdf"
|
121 |
+
)
|
122 |
+
|
123 |
+
# Cleanup the processed output PDF
|
124 |
+
if os.path.exists(output_pdf_path):
|
125 |
+
os.remove(output_pdf_path)
|
126 |
+
|
127 |
+
|
128 |
+
if __name__ == "__main__":
|
129 |
+
main()
|