Skip to main content
Open In ColabOpen on GitHub

Microsoft Word

Microsoft Word 是一款由 Microsoft 开发的文字处理器。

这涵盖了如何将 Word 文档加载到下游可用的文档格式中。

使用 Docx2txt

使用 Docx2txt 将 .docx 加载到文档中。

%pip install --upgrade --quiet  docx2txt
from langchain_community.document_loaders import Docx2txtLoader

loader = Docx2txtLoader("./example_data/fake.docx")

data = loader.load()

data
API Reference:Docx2txtLoader
[Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': './example_data/fake.docx'})]

使用 Unstructured

请参阅 此指南,了解在本地设置 Unstructured 的更多说明,包括设置所需的系统依赖项。

from langchain_community.document_loaders import UnstructuredWordDocumentLoader

loader = UnstructuredWordDocumentLoader("example_data/fake.docx")

data = loader.load()

data
[Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': 'example_data/fake.docx'})]

保留元素

在底层,Unstructured 会为不同的文本块创建不同的“元素”。默认情况下,我们会将它们组合在一起,但您可以通过指定 mode="elements" 来轻松地保留这种分离。

loader = UnstructuredWordDocumentLoader("./example_data/fake.docx", mode="elements")

data = loader.load()

data[0]
Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': './example_data/fake.docx', 'category_depth': 0, 'file_directory': './example_data', 'filename': 'fake.docx', 'last_modified': '2023-12-19T13:42:18', 'languages': ['por', 'cat'], 'filetype': 'application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'category': 'Title'})

使用 Azure AI Document Intelligence

Azure AI Document Intelligence (以前称为 Azure Form Recognizer) 是一种基于机器学习的服务,可以从数字或扫描的 PDF、图像、Office 和 HTML 文件中提取文本(包括手写体)、表格、文档结构(例如标题、章节标题等)以及键值对。

Document Intelligence 支持 PDFJPEG/JPGPNGBMPTIFFHEIFDOCXXLSXPPTXHTML

目前使用 Document Intelligence 的加载器的实现可以按页整合内容,并将其转换为 LangChain 文档。默认输出格式为 markdown,可以轻松地与 MarkdownHeaderTextSplitter 链接,以实现语义化的文档分块。您还可以使用 mode="single"mode="page" 以单页纯文本或按页分割的文档形式返回。

先决条件

在以下 3 个预览区域之一中创建一个 Azure AI Document Intelligence 资源:美国东部美国西部 2欧洲西部 - 如果您还没有资源,请遵循此文档进行创建。您将把 <endpoint><key> 作为参数传递给加载程序。

%pip install --upgrade --quiet langchain langchain-community azure-ai-documentintelligence

from langchain_community.document_loaders import AzureAIDocumentIntelligenceLoader

file_path = "<filepath>"
endpoint = "<endpoint>"
key = "<key>"
loader = AzureAIDocumentIntelligenceLoader(
api_endpoint=endpoint, api_key=key, file_path=file_path, api_model="prebuilt-layout"
)

documents = loader.load()