Skip to main content
Open In Colab在 GitHub 上打开

Microsoft Word

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

这涵盖了如何加载Worddocuments 转换为我们可以在下游使用的文档格式。

使用 Docx2txt

加载.docxDocx2txt导入到文档中。

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

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

data = loader.load()

data
API 参考: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 支持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()