跳到主要内容
本笔记本展示了如何加载电子邮件 (.eml) 或 Microsoft Outlook (.msg) 文件。 关于在本地设置 Unstructured 的更多说明,包括设置所需的系统依赖项,请参阅本指南

使用 Unstructured

pip install -qU unstructured
from langchain_community.document_loaders import UnstructuredEmailLoader

loader = UnstructuredEmailLoader("./example_data/fake-email.eml")

data = loader.load()

data
[Document(page_content='This is a test email to use for unit tests.\n\nImportant points:\n\nRoses are red\n\nViolets are blue', metadata={'source': './example_data/fake-email.eml'})]

保留元素

在底层,Unstructured 为不同的文本块创建不同的“元素”。默认情况下,我们会将这些元素组合在一起,但您可以通过指定 mode="elements" 轻松地保留这种分离。
loader = UnstructuredEmailLoader("example_data/fake-email.eml", mode="elements")

data = loader.load()

data[0]
Document(page_content='This is a test email to use for unit tests.', metadata={'source': 'example_data/fake-email.eml', 'file_directory': 'example_data', 'filename': 'fake-email.eml', 'last_modified': '2022-12-16T17:04:16-05:00', 'sent_from': ['Matthew Robinson [mrobinson@unstructured.io](mailto:mrobinson@unstructured.io)'], 'sent_to': ['Matthew Robinson [mrobinson@unstructured.io](mailto:mrobinson@unstructured.io)'], 'subject': 'Test Email', 'languages': ['eng'], 'filetype': 'message/rfc822', 'category': 'NarrativeText'})

处理附件

您可以通过在构造函数中设置 process_attachments=True 来使用 UnstructuredEmailLoader 处理附件。默认情况下,附件将使用 unstructured 中的 partition 函数进行分区。您可以通过将函数传递给 attachment_partitioner 关键字参数来使用不同的分区函数。
loader = UnstructuredEmailLoader(
    "example_data/fake-email.eml",
    mode="elements",
    process_attachments=True,
)

data = loader.load()

data[0]
Document(page_content='This is a test email to use for unit tests.', metadata={'source': 'example_data/fake-email.eml', 'file_directory': 'example_data', 'filename': 'fake-email.eml', 'last_modified': '2022-12-16T17:04:16-05:00', 'sent_from': ['Matthew Robinson [mrobinson@unstructured.io](mailto:mrobinson@unstructured.io)'], 'sent_to': ['Matthew Robinson [mrobinson@unstructured.io](mailto:mrobinson@unstructured.io)'], 'subject': 'Test Email', 'languages': ['eng'], 'filetype': 'message/rfc822', 'category': 'NarrativeText'})

使用 OutlookMessageLoader

pip install -qU extract_msg
from langchain_community.document_loaders import OutlookMessageLoader

loader = OutlookMessageLoader("example_data/fake-email.msg")

data = loader.load()

data[0]
Document(page_content='This is a test email to experiment with the MS Outlook MSG Extractor\r\n\r\n\r\n-- \r\n\r\n\r\nKind regards\r\n\r\n\r\n\r\n\r\nBrian Zhou\r\n\r\n', metadata={'source': 'example_data/fake-email.msg', 'subject': 'Test for TIF files', 'sender': 'Brian Zhou [brizhou@gmail.com](mailto:brizhou@gmail.com)', 'date': datetime.datetime(2013, 11, 18, 0, 26, 24, tzinfo=zoneinfo.ZoneInfo(key='America/Los_Angeles'))})

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