Spaces:
Running
Running
File size: 1,018 Bytes
1540d77 b4b5aad 1540d77 dade368 1540d77 dade368 b4b5aad 1540d77 |
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 |
"""
File manager helper to work with uploaded files.
"""
import logging
import os
import sys
import streamlit as st
from pypdf import PdfReader
sys.path.append('..')
sys.path.append('../..')
from global_config import GlobalConfig
logger = logging.getLogger(__name__)
def get_pdf_contents(
pdf_file: st.runtime.uploaded_file_manager.UploadedFile,
page_range: tuple[int, int],
max_pages: int = GlobalConfig.MAX_PAGE_COUNT
) -> str:
"""
Extract the text contents from a PDF file.
:param pdf_file: The uploaded PDF file.
:param page_range: The range of pages to extract contents from.
:param max_pages: The max no. of pages to extract contents from.
:return: The contents.
"""
reader = PdfReader(pdf_file)
start, end = page_range # set start and end per the range (user-specified values)
text = ''
for page_num in range(start - 1, end):
page = reader.pages[page_num]
text += page.extract_text()
return text
|