File size: 2,025 Bytes
e3bed55
 
dd6a995
e3bed55
bacffa7
6acdb94
e3bed55
b1395a5
e3bed55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
96c6cb4
bacffa7
6acdb94
b189c38
df96733
e3bed55
ce79ee6
 
 
3a7ceb5
 
723a5a6
 
 
 
 
 
 
ce79ee6
 
 
 
 
 
 
 
0018abe
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import streamlit as st
import PyPDF2
from io import BytesIO



def search_pdf(pdf_file, search_term):
    with BytesIO(pdf_file.read()) as file:
        reader = PyPDF2.PdfReader(file)
        num_pages = len(reader.pages)
        search_results = []

        for page_num in range(num_pages):
            page = reader.pages[page_num]
            text = page.extract_text()
            if search_term.lower() in text.lower():
                search_results.append((page_num + 1, text))
    return search_results

def final_result(pdf_file, search_term):
    results = search_pdf(pdf_file, search_term)
    output_text = ""
    if results:
        for page_num, text in results:
            # output_text += f"Found \033[1m{search_term}\033[0m on page {page_num}:\n{text}\n\n"
            output_text += f"Found '{search_term}' on page {page_num}:\n{text}\n\n"
    else:
        output_text = f"No results found for '{search_term}'."
    return output_text
    

# st.markdown("<h3 style='text-align:center; font-size:24px;'>Search in PDF</h3>", unsafe_allow_html=True)
st.set_page_config(page_title="Search in PDF", layout="wide",initial_sidebar_state="expanded")

col1, col2 = st.columns(spec=[0.4,0.6])
# col3, col4 = st.columns(spec=[0.5,0.5])

with col1:
    input_file = st.file_uploader(label="Upload .pdf File", type='pdf')
    search_term = st.text_input(label="Enter Search-term", placeholder="Search here...")
    col3, col4 = st.columns()
    with col3:
        all_data = st.button("Submit")
    with col4:
        st.write("")
        clear_button = st.button("Clear")
        if clear_button:
            input_file = None
            search_term = ""
with col2:
    if all_data:
        if input_file is not None and search_term.strip() != "":
            result = final_result(input_file, search_term)
            st.success(result)
        elif input_file is None:
            st.error("Please upload a PDF file.")
        elif search_term.strip() == "":
            st.error("Please enter a search term.")