mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-03 21:54:04 +00:00
feat: add UnstructuredExcelLoader
for .xlsx
and .xls
files (#5617)
# Unstructured Excel Loader Adds an `UnstructuredExcelLoader` class for `.xlsx` and `.xls` files. Works with `unstructured>=0.6.7`. A plain text representation of the Excel file will be available under the `page_content` attribute in the doc. If you use the loader in `"elements"` mode, an HTML representation of the Excel file will be available under the `text_as_html` metadata key. Each sheet in the Excel document is its own document. ### Testing ```python from langchain.document_loaders import UnstructuredExcelLoader loader = UnstructuredExcelLoader( "example_data/stanley-cups.xlsx", mode="elements" ) docs = loader.load() ``` ## Who can review? @hwchase17 @eyurtsev
This commit is contained in:
parent
9a7488a5ce
commit
a97e4252e3
Binary file not shown.
79
docs/modules/indexes/document_loaders/examples/excel.ipynb
Normal file
79
docs/modules/indexes/document_loaders/examples/excel.ipynb
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"id": "22a849cc",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Microsoft Excel\n",
|
||||||
|
"\n",
|
||||||
|
"The `UnstructuredExcelLoader` is used to load `Microsoft Excel` files. The loader works with both `.xlsx` and `.xls` files. The page content will be the raw text of the Excel file. If you use the loader in `\"elements\"` mode, an HTML representation of the Excel file will be available in the document metadata under the `text_as_html` key."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 1,
|
||||||
|
"id": "e6616e3a",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from langchain.document_loaders import UnstructuredExcelLoader"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": 2,
|
||||||
|
"id": "a654e4d9",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [
|
||||||
|
{
|
||||||
|
"data": {
|
||||||
|
"text/plain": [
|
||||||
|
"Document(page_content='\\n \\n \\n Team\\n Location\\n Stanley Cups\\n \\n \\n Blues\\n STL\\n 1\\n \\n \\n Flyers\\n PHI\\n 2\\n \\n \\n Maple Leafs\\n TOR\\n 13\\n \\n \\n', metadata={'source': 'example_data/stanley-cups.xlsx', 'filename': 'stanley-cups.xlsx', 'file_directory': 'example_data', 'filetype': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'page_number': 1, 'page_name': 'Stanley Cups', 'text_as_html': '<table border=\"1\" class=\"dataframe\">\\n <tbody>\\n <tr>\\n <td>Team</td>\\n <td>Location</td>\\n <td>Stanley Cups</td>\\n </tr>\\n <tr>\\n <td>Blues</td>\\n <td>STL</td>\\n <td>1</td>\\n </tr>\\n <tr>\\n <td>Flyers</td>\\n <td>PHI</td>\\n <td>2</td>\\n </tr>\\n <tr>\\n <td>Maple Leafs</td>\\n <td>TOR</td>\\n <td>13</td>\\n </tr>\\n </tbody>\\n</table>', 'category': 'Table'})"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"execution_count": 2,
|
||||||
|
"metadata": {},
|
||||||
|
"output_type": "execute_result"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"source": [
|
||||||
|
"loader = UnstructuredExcelLoader(\n",
|
||||||
|
" \"example_data/stanley-cups.xlsx\",\n",
|
||||||
|
" mode=\"elements\"\n",
|
||||||
|
")\n",
|
||||||
|
"docs = loader.load()\n",
|
||||||
|
"docs[0]"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"id": "9ab94bde",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": []
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3 (ipykernel)",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python3"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.8.13"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 5
|
||||||
|
}
|
@ -32,6 +32,7 @@ from langchain.document_loaders.email import (
|
|||||||
)
|
)
|
||||||
from langchain.document_loaders.epub import UnstructuredEPubLoader
|
from langchain.document_loaders.epub import UnstructuredEPubLoader
|
||||||
from langchain.document_loaders.evernote import EverNoteLoader
|
from langchain.document_loaders.evernote import EverNoteLoader
|
||||||
|
from langchain.document_loaders.excel import UnstructuredExcelLoader
|
||||||
from langchain.document_loaders.facebook_chat import FacebookChatLoader
|
from langchain.document_loaders.facebook_chat import FacebookChatLoader
|
||||||
from langchain.document_loaders.figma import FigmaFileLoader
|
from langchain.document_loaders.figma import FigmaFileLoader
|
||||||
from langchain.document_loaders.gcs_directory import GCSDirectoryLoader
|
from langchain.document_loaders.gcs_directory import GCSDirectoryLoader
|
||||||
@ -223,6 +224,7 @@ __all__ = [
|
|||||||
"UnstructuredAPIFileLoader",
|
"UnstructuredAPIFileLoader",
|
||||||
"UnstructuredEPubLoader",
|
"UnstructuredEPubLoader",
|
||||||
"UnstructuredEmailLoader",
|
"UnstructuredEmailLoader",
|
||||||
|
"UnstructuredExcelLoader",
|
||||||
"UnstructuredFileIOLoader",
|
"UnstructuredFileIOLoader",
|
||||||
"UnstructuredFileLoader",
|
"UnstructuredFileLoader",
|
||||||
"UnstructuredHTMLLoader",
|
"UnstructuredHTMLLoader",
|
||||||
|
22
langchain/document_loaders/excel.py
Normal file
22
langchain/document_loaders/excel.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
"""Loader that loads Microsoft Excel files."""
|
||||||
|
from typing import Any, List
|
||||||
|
|
||||||
|
from langchain.document_loaders.unstructured import (
|
||||||
|
UnstructuredFileLoader,
|
||||||
|
validate_unstructured_version,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class UnstructuredExcelLoader(UnstructuredFileLoader):
|
||||||
|
"""Loader that uses unstructured to load Microsoft Excel files."""
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, file_path: str, mode: str = "single", **unstructured_kwargs: Any
|
||||||
|
):
|
||||||
|
validate_unstructured_version(min_unstructured_version="0.6.7")
|
||||||
|
super().__init__(file_path=file_path, mode=mode, **unstructured_kwargs)
|
||||||
|
|
||||||
|
def _get_elements(self) -> List:
|
||||||
|
from unstructured.partition.xlsx import partition_xlsx
|
||||||
|
|
||||||
|
return partition_xlsx(filename=self.file_path, **self.unstructured_kwargs)
|
15
tests/integration_tests/document_loaders/test_excel.py
Normal file
15
tests/integration_tests/document_loaders/test_excel.py
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from langchain.document_loaders import UnstructuredExcelLoader
|
||||||
|
|
||||||
|
EXAMPLE_DIRECTORY = file_path = Path(__file__).parent.parent / "examples"
|
||||||
|
|
||||||
|
|
||||||
|
def test_unstructured_excel_loader() -> None:
|
||||||
|
"""Test unstructured loader."""
|
||||||
|
file_path = os.path.join(EXAMPLE_DIRECTORY, "stanley-cups.xlsx")
|
||||||
|
loader = UnstructuredExcelLoader(str(file_path))
|
||||||
|
docs = loader.load()
|
||||||
|
|
||||||
|
assert len(docs) == 1
|
BIN
tests/integration_tests/examples/stanley-cups.xlsx
Normal file
BIN
tests/integration_tests/examples/stanley-cups.xlsx
Normal file
Binary file not shown.
Loading…
Reference in New Issue
Block a user