跳到主要內容
Open In ColabOpen on GitHub

Microsoft Word

Microsoft Word 是由 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
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 文件智慧

Azure AI 文件智慧(前身為 Azure Form Recognizer)是基於機器學習的服務,可從數位或掃描的 PDF、圖像、Office 和 HTML 檔案中擷取文字(包括手寫)、表格、文件結構(例如,標題、章節標題等)和鍵值對。

Document Intelligence 支援 PDFJPEG/JPGPNGBMPTIFFHEIFDOCXXLSXPPTXHTML

目前使用 Document Intelligence 的載入器實作可以逐頁整合內容,並將其轉換為 LangChain 文件。預設輸出格式為 markdown,可以輕鬆地與 MarkdownHeaderTextSplitter 鏈結,以進行語意文件分塊。您也可以使用 mode="single"mode="page" 來傳回單頁或按頁面分割的文件中的純文字。

先決條件

在 3 個預覽區域之一中建立 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()

此頁面是否有幫助?