2023-04-06 14:35:22 -04:00
|
|
|
from typing import IO, Callable, Optional
|
2023-01-09 16:15:14 -05:00
|
|
|
|
2023-02-27 17:30:54 +01:00
|
|
|
from unstructured.file_utils.filetype import FileType, detect_filetype
|
2023-02-17 09:30:23 -05:00
|
|
|
from unstructured.partition.doc import partition_doc
|
2023-01-09 16:15:14 -05:00
|
|
|
from unstructured.partition.docx import partition_docx
|
|
|
|
from unstructured.partition.email import partition_email
|
2023-03-14 11:52:21 -04:00
|
|
|
from unstructured.partition.epub import partition_epub
|
2023-01-09 16:15:14 -05:00
|
|
|
from unstructured.partition.html import partition_html
|
2023-02-27 17:30:54 +01:00
|
|
|
from unstructured.partition.image import partition_image
|
2023-03-09 03:36:01 +09:00
|
|
|
from unstructured.partition.json import partition_json
|
2023-02-27 23:36:44 +01:00
|
|
|
from unstructured.partition.md import partition_md
|
2023-03-28 16:15:22 -04:00
|
|
|
from unstructured.partition.msg import partition_msg
|
2023-01-09 16:15:14 -05:00
|
|
|
from unstructured.partition.pdf import partition_pdf
|
2023-02-17 11:57:08 -05:00
|
|
|
from unstructured.partition.ppt import partition_ppt
|
2023-01-23 12:03:09 -05:00
|
|
|
from unstructured.partition.pptx import partition_pptx
|
2023-01-13 16:39:53 -05:00
|
|
|
from unstructured.partition.text import partition_text
|
2023-01-09 16:15:14 -05:00
|
|
|
|
|
|
|
|
2023-02-08 10:11:15 -05:00
|
|
|
def partition(
|
|
|
|
filename: Optional[str] = None,
|
2023-03-24 16:32:45 -07:00
|
|
|
content_type: Optional[str] = None,
|
2023-02-08 10:11:15 -05:00
|
|
|
file: Optional[IO] = None,
|
2023-03-24 16:32:45 -07:00
|
|
|
file_filename: Optional[str] = None,
|
2023-02-08 10:11:15 -05:00
|
|
|
include_page_breaks: bool = False,
|
2023-03-10 22:16:05 -05:00
|
|
|
strategy: str = "hi_res",
|
|
|
|
encoding: str = "utf-8",
|
2023-04-06 14:35:22 -04:00
|
|
|
paragraph_grouper: Optional[Callable[[str], str]] = None,
|
2023-02-08 10:11:15 -05:00
|
|
|
):
|
2023-01-09 16:15:14 -05:00
|
|
|
"""Partitions a document into its constituent elements. Will use libmagic to determine
|
|
|
|
the file's type and route it to the appropriate partitioning function. Applies the default
|
|
|
|
parameters for each partitioning function. Use the document-type specific partitioning
|
|
|
|
functions if you need access to additional kwarg options.
|
|
|
|
|
|
|
|
Parameters
|
|
|
|
----------
|
2023-04-05 10:54:11 -07:00
|
|
|
filename
|
2023-01-09 16:15:14 -05:00
|
|
|
A string defining the target filename path.
|
2023-03-24 16:32:45 -07:00
|
|
|
content_type
|
|
|
|
A string defining the file content in MIME type
|
2023-01-09 16:15:14 -05:00
|
|
|
file
|
|
|
|
A file-like object using "rb" mode --> open(filename, "rb").
|
2023-03-24 16:32:45 -07:00
|
|
|
file_filename
|
|
|
|
When file is not None, the filename (string) to store in element metadata. E.g. "foo.txt"
|
2023-02-08 10:11:15 -05:00
|
|
|
include_page_breaks
|
|
|
|
If True, the output will include page breaks if the filetype supports it
|
2023-03-10 22:16:05 -05:00
|
|
|
strategy
|
|
|
|
The strategy to use for partitioning the PDF. Uses a layout detection model if set
|
|
|
|
to 'hi_res', otherwise partition_pdf simply extracts the text from the document
|
|
|
|
and processes it.
|
|
|
|
encoding
|
|
|
|
The encoding method used to decode the text input. If None, utf-8 will be used.
|
2023-01-09 16:15:14 -05:00
|
|
|
"""
|
2023-03-24 16:32:45 -07:00
|
|
|
filetype = detect_filetype(
|
|
|
|
filename=filename,
|
|
|
|
file=file,
|
|
|
|
file_filename=file_filename,
|
|
|
|
content_type=content_type,
|
|
|
|
)
|
2023-01-09 16:15:14 -05:00
|
|
|
|
|
|
|
if file is not None:
|
|
|
|
file.seek(0)
|
|
|
|
|
2023-02-17 09:30:23 -05:00
|
|
|
if filetype == FileType.DOC:
|
|
|
|
return partition_doc(filename=filename, file=file)
|
2023-01-09 16:15:14 -05:00
|
|
|
if filetype == FileType.DOCX:
|
|
|
|
return partition_docx(filename=filename, file=file)
|
|
|
|
elif filetype == FileType.EML:
|
2023-03-10 22:16:05 -05:00
|
|
|
return partition_email(filename=filename, file=file, encoding=encoding)
|
2023-03-28 16:15:22 -04:00
|
|
|
elif filetype == FileType.MSG:
|
|
|
|
return partition_msg(filename=filename, file=file)
|
2023-01-09 16:15:14 -05:00
|
|
|
elif filetype == FileType.HTML:
|
2023-03-10 22:16:05 -05:00
|
|
|
return partition_html(
|
|
|
|
filename=filename,
|
|
|
|
file=file,
|
|
|
|
include_page_breaks=include_page_breaks,
|
|
|
|
encoding=encoding,
|
|
|
|
)
|
2023-03-14 11:52:21 -04:00
|
|
|
elif filetype == FileType.EPUB:
|
|
|
|
return partition_epub(filename=filename, file=file, include_page_breaks=include_page_breaks)
|
2023-02-27 23:36:44 +01:00
|
|
|
elif filetype == FileType.MD:
|
|
|
|
return partition_md(filename=filename, file=file, include_page_breaks=include_page_breaks)
|
2023-01-09 16:15:14 -05:00
|
|
|
elif filetype == FileType.PDF:
|
2023-02-08 10:11:15 -05:00
|
|
|
return partition_pdf(
|
|
|
|
filename=filename, # type: ignore
|
|
|
|
file=file, # type: ignore
|
|
|
|
url=None,
|
|
|
|
include_page_breaks=include_page_breaks,
|
2023-03-10 22:16:05 -05:00
|
|
|
encoding=encoding,
|
|
|
|
strategy=strategy,
|
2023-02-08 10:11:15 -05:00
|
|
|
)
|
2023-01-13 22:24:13 -06:00
|
|
|
elif (filetype == FileType.PNG) or (filetype == FileType.JPG):
|
2023-02-08 10:11:15 -05:00
|
|
|
return partition_image(
|
|
|
|
filename=filename, # type: ignore
|
|
|
|
file=file, # type: ignore
|
|
|
|
url=None,
|
|
|
|
include_page_breaks=include_page_breaks,
|
|
|
|
)
|
2023-01-13 16:39:53 -05:00
|
|
|
elif filetype == FileType.TXT:
|
2023-04-06 14:35:22 -04:00
|
|
|
return partition_text(
|
|
|
|
filename=filename,
|
|
|
|
file=file,
|
|
|
|
encoding=encoding,
|
|
|
|
paragraph_grouper=paragraph_grouper,
|
|
|
|
)
|
2023-02-17 11:57:08 -05:00
|
|
|
elif filetype == FileType.PPT:
|
|
|
|
return partition_ppt(filename=filename, file=file, include_page_breaks=include_page_breaks)
|
2023-01-23 12:03:09 -05:00
|
|
|
elif filetype == FileType.PPTX:
|
2023-02-08 10:11:15 -05:00
|
|
|
return partition_pptx(filename=filename, file=file, include_page_breaks=include_page_breaks)
|
2023-03-09 03:36:01 +09:00
|
|
|
elif filetype == FileType.JSON:
|
|
|
|
return partition_json(filename=filename, file=file)
|
2023-01-09 16:15:14 -05:00
|
|
|
else:
|
|
|
|
msg = "Invalid file" if not filename else f"Invalid file {filename}"
|
2023-03-06 16:08:10 -08:00
|
|
|
raise ValueError(f"{msg}. The {filetype} file type is not supported in partition.")
|