Microsoft Word
Microsoft Word is a word processor developed by Microsoft.
这涵盖了如何将 Word 个文档加载为我们可在下游使用的文档格式。
使用 Docx2txt
使用 Docx2txt 将 .docx 加载到文档中。
%pip install --upgrade --quiet 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 的更多说明,包括设置所需的系统依赖项。
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 Document Intelligence (formerly known as
Azure Form Recognizer) is machine-learning based service that extracts texts (including handwriting), tables, document structures (e.g., titles, section headings, etc.) and key-value-pairs from digital or scanned PDFs, images, Office and HTML files.Document Intelligence supports
JPEG/JPG,PNG,BMP,TIFF,HEIF,DOCX,XLSX,PPTXandHTML.
当前使用Document Intelligence的加载器实现可以按页整合内容,并将其转换为 LangChain 文档。默认输出格式为 Markdown,可轻松与MarkdownHeaderTextSplitter链接以实现语义文档分块。您也可以使用mode="single"或mode="page"返回单页纯文本,或返回按页分割的文档。
前置条件
在以下三个预览区域之一的 Azure AI 文档智能资源:美国东部、美国西部2、欧洲西部 - 如果您还没有,请按照此文档创建一个。您将向加载器传递 <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()