Skip to main content
Open on GitHub

如何加载 Microsoft Office 文件

Microsoft Office 套件 中的生产力软件包括 Microsoft Word、Microsoft Excel、Microsoft PowerPoint、Microsoft Outlook 和 Microsoft OneNote。它适用于 Microsoft Windows 和 macOS 操作系统。它也适用于 Android 和 iOS。

本文介绍如何将常用的文件格式,包括DOCXXLSXPPTX文档加载到LangChain的Document对象中,以便在后续流程中使用。

使用 AzureAIDocumentIntelligenceLoader 加载 DOCX、XLSX、PPTX

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

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

前置条件

一个位于以下三个预览区域之一的 Azure AI 文档智能资源:East USWest US2West Europe - 如果您尚未拥有,请遵循此文档进行创建。您将把 <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()