codekingpro/portable-devtools
114k
1"""Loads Microsoft Excel files."""2 3from pathlib import Path4from typing import Any, List, Union5 6from langchain_community.document_loaders.unstructured import (7 UnstructuredFileLoader,8 validate_unstructured_version,9)10 11 12class UnstructuredExcelLoader(UnstructuredFileLoader):13 """Load Microsoft Excel files using `Unstructured`.14 15 Like other16 Unstructured loaders, UnstructuredExcelLoader can be used in both17 "single" and "elements" mode. If you use the loader in "elements"18 mode, each sheet in the Excel file will be an Unstructured Table19 element. If you use the loader in "single" mode, an20 HTML representation of the table will be available in the21 "text_as_html" key in the document metadata.22 23 Examples24 --------25 from langchain_community.document_loaders.excel import UnstructuredExcelLoader26 27 loader = UnstructuredExcelLoader("stanley-cups.xlsx", mode="elements")28 docs = loader.load()29 """30 31 def __init__(32 self,33 file_path: Union[str, Path],34 mode: str = "single",35 **unstructured_kwargs: Any,36 ):37 """38 39 Args:40 file_path: The path to the Microsoft Excel file.41 mode: The mode to use when partitioning the file. See unstructured docs42 for more info. Optional. Defaults to "single".43 **unstructured_kwargs: Keyword arguments to pass to unstructured.44 """45 file_path = str(file_path)46 validate_unstructured_version(min_unstructured_version="0.6.7")47 super().__init__(file_path=file_path, mode=mode, **unstructured_kwargs)48 49 def _get_elements(self) -> List:50 from unstructured.partition.xlsx import partition_xlsx51 52 return partition_xlsx(filename=self.file_path, **self.unstructured_kwargs)53 