Documentation Index
Fetch the complete documentation index at: https://langchain-zh.cn/llms.txt
Use this file to discover all available pages before exploring further.
Microsoft Word 是微软开发的文字处理软件。
本文介绍如何将 Word 文档加载到可供下游使用的文档格式中。
使用 Docx2txt
使用 Docx2txt 加载 .docx 文件为文档。
from langchain_community.document_loaders import Docx2txtLoader
loader = Docx2txtLoader("./example_data/fake.docx")
data = loader.load()
data
[Document(page_content='Lorem ipsum dolor sit amet.', metadata={'source': './example_data/fake.docx'})]
使用 unstructured
有关在本地设置 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 文档智能
Azure AI 文档智能(原名 Azure 表单识别器)是一项基于机器学习的服务,可从数字或扫描的 PDF、图像、Office 和 HTML 文件中提取文本(包括手写内容)、表格、文档结构(例如标题、章节标题等)以及键值对。
文档智能支持 PDF、JPEG/JPG、PNG、BMP、TIFF、HEIF、DOCX、XLSX、PPTX 和 HTML。
当前使用 文档智能 的加载器实现可以按页合并内容并将其转换为 LangChain 文档。默认输出格式为 Markdown,可轻松与 MarkdownHeaderTextSplitter 链式结合,用于语义文档分块。您也可以使用 mode="single" 或 mode="page" 来返回单页纯文本或按页分割的文档。
先决条件
一个位于以下三个预览区域之一的 Azure AI 文档智能资源:美国东部、美国西部 2、西欧——如果您还没有,请按照此文档创建一个。您需要将 <endpoint> 和 <key> 作为参数传递给加载器。
pip install -qU 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()