Source code for langchain_community.document_loaders.parsers.msword
from typing import Iterator
from langchain_core.documents import Document
from langchain_community.document_loaders.base import BaseBlobParser
from langchain_community.document_loaders.blob_loaders import Blob
[docs]class MsWordParser(BaseBlobParser):
"""从一个 blob 中解析 Microsoft Word 文档。"""
[docs] def lazy_parse(self, blob: Blob) -> Iterator[Document]: # type: ignore[valid-type]
"""将Microsoft Word文档解析为文档迭代器。
参数:
blob:要解析的blob。
返回:一个文档的迭代器。
"""
try:
from unstructured.partition.doc import partition_doc
from unstructured.partition.docx import partition_docx
except ImportError as e:
raise ImportError(
"Could not import unstructured, please install with `pip install "
"unstructured`."
) from e
mime_type_parser = {
"application/msword": partition_doc,
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": (
partition_docx
),
}
if blob.mimetype not in ( # type: ignore[attr-defined]
"application/msword",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
):
raise ValueError("This blob type is not supported for this parser.")
with blob.as_bytes_io() as word_document: # type: ignore[attr-defined]
elements = mime_type_parser[blob.mimetype](file=word_document) # type: ignore[attr-defined]
text = "\n\n".join([str(el) for el in elements])
metadata = {"source": blob.source} # type: ignore[attr-defined]
yield Document(page_content=text, metadata=metadata)