v0.0.1
Browse files
app.py
ADDED
|
@@ -0,0 +1,79 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import fitz
|
| 3 |
+
import requests
|
| 4 |
+
from io import BytesIO
|
| 5 |
+
import os
|
| 6 |
+
|
| 7 |
+
# Create directory for storing PDFs
|
| 8 |
+
PDF_STORAGE_DIR = 'pdf_storage'
|
| 9 |
+
os.makedirs(PDF_STORAGE_DIR, exist_ok=True)
|
| 10 |
+
|
| 11 |
+
# Streamlit app
|
| 12 |
+
st.title("PDF Manager")
|
| 13 |
+
|
| 14 |
+
# Sidebar
|
| 15 |
+
st.sidebar.header("PDF Manager")
|
| 16 |
+
st.sidebar.markdown("Manage your PDF files")
|
| 17 |
+
|
| 18 |
+
# Download PDF
|
| 19 |
+
def download_pdf(url):
|
| 20 |
+
response = requests.get(url)
|
| 21 |
+
pdf_file_name = url.split('/')[-1]
|
| 22 |
+
with open(os.path.join(PDF_STORAGE_DIR, pdf_file_name), 'wb+') as destination:
|
| 23 |
+
for chunk in response.iter_content(1024):
|
| 24 |
+
destination.write(chunk)
|
| 25 |
+
return pdf_file_name
|
| 26 |
+
|
| 27 |
+
# Upload PDF
|
| 28 |
+
def upload_pdf(pdf_file):
|
| 29 |
+
with open(os.path.join(PDF_STORAGE_DIR, pdf_file.name), 'wb+') as destination:
|
| 30 |
+
for chunk in pdf_file.chunks():
|
| 31 |
+
destination.write(chunk)
|
| 32 |
+
return pdf_file.name
|
| 33 |
+
|
| 34 |
+
# Parse PDF
|
| 35 |
+
def parse_pdf(pdf_file_name, page_numbers, search_string, rect_coords):
|
| 36 |
+
doc = fitz.open(os.path.join(PDF_STORAGE_DIR, pdf_file_name))
|
| 37 |
+
pages = [doc.load_page(int(i)) for i in page_numbers.split(',')]
|
| 38 |
+
text = ''
|
| 39 |
+
for page in pages:
|
| 40 |
+
text += page.get_text()
|
| 41 |
+
if search_string:
|
| 42 |
+
for page in pages:
|
| 43 |
+
rect = page.search_for(search_string)
|
| 44 |
+
if rect:
|
| 45 |
+
text += f'\nSearch result: {rect[0]}'
|
| 46 |
+
if rect_coords:
|
| 47 |
+
x1, y1, x2, y2 = map(float, rect_coords.split(','))
|
| 48 |
+
rect = fitz.Rect(x1, y1, x2, y2)
|
| 49 |
+
for page in pages:
|
| 50 |
+
text += f'\nRect text: {page.get_text("text", clip=rect)}'
|
| 51 |
+
return text
|
| 52 |
+
|
| 53 |
+
# Main app
|
| 54 |
+
def main():
|
| 55 |
+
st.write("### PDF Manager")
|
| 56 |
+
st.write("#### Download PDF")
|
| 57 |
+
url = st.text_input("Enter PDF URL")
|
| 58 |
+
if st.button("Download PDF"):
|
| 59 |
+
pdf_file_name = download_pdf(url)
|
| 60 |
+
st.success(f"PDF downloaded: {pdf_file_name}")
|
| 61 |
+
|
| 62 |
+
st.write("#### Upload PDF")
|
| 63 |
+
pdf_file = st.file_uploader("Select PDF file")
|
| 64 |
+
if pdf_file:
|
| 65 |
+
pdf_file_name = upload_pdf(pdf_file)
|
| 66 |
+
st.success(f"PDF uploaded: {pdf_file_name}")
|
| 67 |
+
|
| 68 |
+
st.write("#### Parse PDF")
|
| 69 |
+
pdf_files = os.listdir(PDF_STORAGE_DIR)
|
| 70 |
+
pdf_file_name = st.selectbox("Select PDF file", pdf_files)
|
| 71 |
+
page_numbers = st.text_input("Enter page numbers (comma-separated)")
|
| 72 |
+
search_string = st.text_input("Enter search string")
|
| 73 |
+
rect_coords = st.text_input("Enter rectangle coordinates (x1,y1,x2,y2)")
|
| 74 |
+
if st.button("Parse PDF"):
|
| 75 |
+
text = parse_pdf(pdf_file_name, page_numbers, search_string, rect_coords)
|
| 76 |
+
st.write(text)
|
| 77 |
+
|
| 78 |
+
if __name__ == "__main__":
|
| 79 |
+
main()
|