Skip to main content
在 GitHub 上打开

如何加载 Microsoft Office 文件

Microsoft Office 生产力软件套件包括 Microsoft Word、Microsoft Excel、Microsoft PowerPoint、Microsoft Outlook 和 Microsoft OneNote。它适用于 Microsoft Windows 和 macOS作系统。它也可以在 Android 和 iOS 上使用。

这涵盖了如何加载常用的文件格式,包括DOCX,XLSXPPTXdocuments 复制到我们可以在下游使用的 LangChain Document 对象中。

使用 AzureAIDocumentIntelligenceLoader 加载 DOCX、XLSX、PPTX

Azure AI Document Intelligence(以前称为Azure Form Recognizer) 是机器学习 基于从中提取文本(包括手写)、表格、文档结构(例如标题、章节标题等)和键值对的服务 数字或扫描的 PDF、图像、Office 和 HTML 文件。Document Intelligence 支持PDF,JPEG/JPG,PNG,BMP,TIFF,HEIF,DOCX,XLSX,PPTXHTML.

当前使用Document Intelligence可以逐页合并内容并将其转换为 LangChain 文档。默认输出格式是 markdown,可以很容易地与MarkdownHeaderTextSplitter用于语义文档分块。您还可以使用mode="single"mode="page"返回单页或按页拆分的文档中的纯文本。

先决条件

以下 3 个预览区域之一的 Azure AI Document Intelligence 资源:美国东部美国西部 2西欧 - 如果没有,请按照此文档创建一个。您将通过<endpoint><key>作为 loader 的参数。

%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()