Мой текущий код работает для жестко закодированных путей к файлам для допустимых входных файлов:
Код: Выделить всё
def extract_from_file(uploaded_file):
file_type = os.path.splitext(uploaded_file.name)[1].lower()
if file_type == ".pdf":
text = pdf_to_text(uploaded_file)
elif file_type in [".doc", ".docx", ".docm", ".dot", ".dotx", ".dotm"]:
text = doc_to_text(uploaded_file)
elif file_type in [".ppt", ".pptx", ".pps", ".ppsx"]:
text = ppt_to_text(uploaded_file)
elif file_type in [".md", ".html", ".htm"]:
text = html_to_text(uploaded_file, file_type)
elif file_type == ".txt":
# adapted from https://www.geeksforgeeks.org/pandas/read-html-file-in-python-using-pandas/
with open(uploaded_file, "r", encoding="utf-8") as f:
text = f.read()
else:
raise ValueError("Unsupported file type: " + file_type)
if text:
return article_from_text(text)
else:
raise ValueError("No text could be extracted from the file.")
Код: Выделить всё
def pdf_to_text(file):
reader = PdfReader(file.file)
return "".join([page.extract_text() for page in reader.pages])
def doc_to_text(file):
document = Document()
document.LoadFromFile(file)
text = document.GetText()
document.Close()
return text
def ppt_to_text(file):
presentation = Presentation()
presentation.LoadFromFile(file)
sb = []
# Loop through all slides and extract test to sb list - O(n^3) - maybe better way to do later? - quite slow
# based on https://github.com/eiceblue/Spire.Presentation-for-Python/blob/main/Python%20Examples/02_ParagraphAndText/ExtractText.py
for slide in presentation.Slides:
for shape in slide.Shapes:
if isinstance(shape, IAutoShape):
for tp in ( shape if isinstance(shape, IAutoShape) else None).TextFrame.Paragraphs:
sb.append (tp.Text)
text = "\n".join(sb)
presentation.Dispose() # Releases all resources used by presentation object
return text
def html_to_text(file, type):
with open(file, "r", encoding="utf-8") as f:
file_content = f.read()
# Convert markdown to html if needed
if type == ".md":
file_content = markdown(file_content)
# from https://gist.github.com/lorey/eb15a7f3338f959a78cc3661fbc255fe
soup = BeautifulSoup(file_content, "html.parser")
return "\n".join(soup.find_all(string=True))
"plum.function.NotFoundLookupError: Для функции "LoadFromFile" из spire.doc.interface.IDocument.IDocument, подпись подписи(spire.doc.Document.Document, django.core.files.uploadedfile.InMemoryUploadedFile) не удалось разрешить."
Я не хочу загружать файл локально, чтобы он работал (что я уже пробовал). Я попытался создать временный файл внутри кода, используя
Код: Выделить всё
from django.core.files.temp import NamedTemporaryFile
temp_file = NamedTemporaryFile(delete=True)
Мне нигде не удалось найти ничего по этому поводу.
Подробнее здесь: https://stackoverflow.com/questions/798 ... -require-a
Мобильная версия