Files
langchain/docs/extras/integrations/document_loaders/microsoft_word.ipynb
T

4.4 KiB

Microsoft Word

Microsoft Word is a word processor developed by Microsoft.

This covers how to load Word documents into a document format that we can use downstream.

Using Docx2txt

Load .docx using Docx2txt into a document.

In [2]:
!pip install docx2txt
In [3]:
from langchain.document_loaders import Docx2txtLoader
In [5]:
loader = Docx2txtLoader("example_data/fake.docx")
In [6]:
data = loader.load()
In [7]:
data
Out [7]:
[Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': 'example_data/fake.docx'})]

Using Unstructured

In [8]:
from langchain.document_loaders import UnstructuredWordDocumentLoader
In [2]:
loader = UnstructuredWordDocumentLoader("example_data/fake.docx")
In [3]:
data = loader.load()
In [4]:
data
Out [4]:
[Document(page_content='Lorem ipsum dolor sit amet.', lookup_str='', metadata={'source': 'fake.docx'}, lookup_index=0)]

Retain Elements

Under the hood, Unstructured creates different "elements" for different chunks of text. By default we combine those together, but you can easily keep that separation by specifying mode="elements".

In [5]:
loader = UnstructuredWordDocumentLoader("example_data/fake.docx", mode="elements")
In [6]:
data = loader.load()
In [7]:
data[0]
Out [7]:
Document(page_content='Lorem ipsum dolor sit amet.', lookup_str='', metadata={'source': 'fake.docx', 'filename': 'fake.docx', 'category': 'Title'}, lookup_index=0)