codekingpro/portable-devtools
114k
1import os2from pathlib import Path3from typing import Any, List, Union4 5from langchain_community.document_loaders.unstructured import (6 UnstructuredFileLoader,7 validate_unstructured_version,8)9 10 11class UnstructuredPowerPointLoader(UnstructuredFileLoader):12 """Load `Microsoft PowerPoint` files using `Unstructured`.13 14 Works with both .ppt and .pptx files.15 You can run the loader in one of two modes: "single" and "elements".16 If you use "single" mode, the document will be returned as a single17 langchain Document object. If you use "elements" mode, the unstructured18 library will split the document into elements such as Title and NarrativeText.19 You can pass in additional unstructured kwargs after mode to apply20 different unstructured settings.21 22 Examples23 --------24 from langchain_community.document_loaders import UnstructuredPowerPointLoader25 26 loader = UnstructuredPowerPointLoader(27 "example.pptx", mode="elements", strategy="fast",28 )29 docs = loader.load()30 31 References32 ----------33 https://unstructured-io.github.io/unstructured/bricks.html#partition-pptx34 """35 36 def __init__(37 self,38 file_path: Union[str, Path],39 mode: str = "single",40 **unstructured_kwargs: Any,41 ):42 """43 44 Args:45 file_path: The path to the PowerPoint file to load.46 mode: The mode to use when loading the file. Can be one of "single",47 "multi", or "all". Default is "single".48 **unstructured_kwargs: Any kwargs to pass to the unstructured.49 """50 file_path = str(file_path)51 super().__init__(file_path=file_path, mode=mode, **unstructured_kwargs)52 53 def _get_elements(self) -> List:54 from unstructured.file_utils.filetype import FileType, detect_filetype55 56 # NOTE(MthwRobinson) - magic will raise an import error if the libmagic57 # system dependency isn't installed. If it's not installed, we'll just58 # check the file extension59 try:60 import magic # noqa: F40161 62 is_ppt = detect_filetype(self.file_path) == FileType.PPT63 except ImportError:64 _, extension = os.path.splitext(str(self.file_path))65 is_ppt = extension == ".ppt"66 67 if is_ppt:68 validate_unstructured_version("0.4.11")69 70 if is_ppt:71 from unstructured.partition.ppt import partition_ppt72 73 return partition_ppt(filename=self.file_path, **self.unstructured_kwargs)74 else:75 from unstructured.partition.pptx import partition_pptx76 77 return partition_pptx(filename=self.file_path, **self.unstructured_kwargs)78 