跳到主要内容
Microsoft Word 是 Microsoft 开发的一款文字处理器。
本文档介绍了如何将 Word 文档加载成可供后续使用的文档格式。

使用 Docx2txt

使用 Docx2txt 将 .docx 文件加载到文档中。
pip install -qU  docx2txt
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 的更多说明,包括设置所需的系统依赖项。
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 -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()

以编程方式连接这些文档到 Claude、VSCode 等,通过 MCP 获取实时答案。
© . This site is unofficial and not affiliated with LangChain, Inc.