Microsoft PowerPoint
Microsoft PowerPoint 是 Microsoft 公司的簡報程式。
本節介紹如何將 Microsoft PowerPoint
文件載入為我們可以下游使用的文件格式。
請參閱本指南,以取得更多關於在本地設定 Unstructured 的指示,包括設定所需的系統依賴項。
# Install packages
%pip install unstructured
%pip install python-magic
%pip install python-pptx
from langchain_community.document_loaders import UnstructuredPowerPointLoader
loader = UnstructuredPowerPointLoader("./example_data/fake-power-point.pptx")
data = loader.load()
data
API 參考:UnstructuredPowerPointLoader
[Document(page_content='Adding a Bullet Slide\n\nFind the bullet slide layout\n\nUse _TextFrame.text for first bullet\n\nUse _TextFrame.add_paragraph() for subsequent bullets\n\nHere is a lot of text!\n\nHere is some text in a text box!', metadata={'source': './example_data/fake-power-point.pptx'})]
保留元素
在底層,Unstructured
會為不同的文字區塊建立不同的「元素」。 預設情況下,我們會將它們組合在一起,但您可以透過指定 mode="elements"
輕鬆地保留這種分隔。
loader = UnstructuredPowerPointLoader(
"./example_data/fake-power-point.pptx", mode="elements"
)
data = loader.load()
data[0]
Document(page_content='Adding a Bullet Slide', metadata={'source': './example_data/fake-power-point.pptx', 'category_depth': 0, 'file_directory': './example_data', 'filename': 'fake-power-point.pptx', 'last_modified': '2023-12-19T13:42:18', 'page_number': 1, 'languages': ['eng'], 'filetype': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'category': 'Title'})
使用 Azure AI 文件智慧
Azure AI 文件智慧 (前身為
Azure Form Recognizer
) 是一項基於機器學習的服務,可從數位或掃描的 PDF、圖像、Office 和 HTML 檔案中提取文字 (包括手寫)、表格、文件結構 (例如,標題、章節標題等) 和鍵值對。文件智慧支援
JPEG/JPG
、PNG
、BMP
、TIFF
、HEIF
、DOCX
、XLSX
、PPTX
和HTML
。
目前使用 Document Intelligence
的載入器實作可以逐頁合併內容,並將其轉換為 LangChain 文件。 預設輸出格式為 Markdown,可以輕鬆地與 MarkdownHeaderTextSplitter
鏈接,以進行語意文件分塊。 您也可以使用 mode="single"
或 mode="page"
來傳回單一頁面或按頁面分割的文件中的純文字。
先決條件
Azure AI 文件智慧資源,位於 3 個預覽區域之一:美國東部、美國西部 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()