Converting PPTX to Langchain Documents Using Unstructured
Unstructured is a robust document processing library that excels at converting various document formats into clean, structured text.
It is well integrated with LangChain's ecosystem and provides reliable document parsing capabilities.
The library includes:
Local processing with open-source package
Remote processing via Unstructured API
Comprehensive document format support
Built-in OCR capabilities
from langchain_community.document_loaders import UnstructuredPowerPointLoader# Initialize UnstructuredPowerPointLoaderloader =UnstructuredPowerPointLoader("data/07-ppt-loader-sample.pptx")# Load PowerPoint documentdocs = loader.load()# Print number of loaded documentsprint(len(docs))
1
print(docs[0].page_content[:100])
Natural Language Processing with Deep Learning
CS224N/Ling284
Christopher Manning
Lecture 2: Word
Unstructured generates various "elements" for different chunks of text.
By default, they are combined and returned as a single document, but elements can be easily separated by specifying mode="elements".
# Create UnstructuredPowerPointLoader with elements modeloader =UnstructuredPowerPointLoader("data/07-ppt-loader-sample.pptx", mode="elements")# Load PowerPoint elementsdocs = loader.load()# Print number of elements extractedprint(len(docs))
498
docs[0]
Document(metadata={'source': 'assets/sample.pptx', 'category_depth': 0, 'file_directory': 'assets', 'filename': 'sample.pptx', 'last_modified': '2024-12-30T01:00:34', 'page_number': 1, 'languages': ['eng'], 'filetype': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'category': 'Title', 'element_id': 'aa75080e026117468068eec241cf786f'}, page_content='Natural Language Processing with Deep Learning')
# Get and display the first elementfirst_element = docs[0]print(first_element)# To see its metadata and content separately, you could do:print("Content:", first_element.page_content)print("Metadata:", first_element.metadata)
page_content='Natural Language Processing with Deep Learning' metadata={'source': 'assets/sample.pptx', 'category_depth': 0, 'file_directory': 'assets', 'filename': 'sample.pptx', 'last_modified': '2024-12-30T01:00:34', 'page_number': 1, 'languages': ['eng'], 'filetype': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'category': 'Title', 'element_id': 'aa75080e026117468068eec241cf786f'}
Content: Natural Language Processing with Deep Learning
Metadata: {'source': 'assets/sample.pptx', 'category_depth': 0, 'file_directory': 'assets', 'filename': 'sample.pptx', 'last_modified': '2024-12-30T01:00:34', 'page_number': 1, 'languages': ['eng'], 'filetype': 'application/vnd.openxmlformats-officedocument.presentationml.presentation', 'category': 'Title', 'element_id': 'aa75080e026117468068eec241cf786f'}
# Print elements with formatted output and enumerate for easy referencefor idx, doc inenumerate(docs[:3], 1):print(f"\nElement {idx}/{len(docs)}")print(f"Category: {doc.metadata['category']}")print("="*50)print(f"Content:\n{doc.page_content.strip()}")print("="*50)
Element 1/498
Category: Title
==================================================
Content:
Natural Language Processing with Deep Learning
==================================================
Element 2/498
Category: Title
==================================================
Content:
CS224N/Ling284
==================================================
Element 3/498
Category: Title
==================================================
Content:
Christopher Manning
==================================================
Converting PPTX to Langchain Documents Using MarkItDown
MarkItDown is an open-source library by Microsoft that converts unstructured documents into structured Markdown, a format that LLMs can easily process and understand. This makes it particularly valuable for RAG (Retrieval Augmented Generation) systems by enabling clean, semantic text representation.
Supporting formats like PDF, PowerPoint, Word, Excel, images (with EXIF/OCR), audio (with transcription), HTML, and more, MarkItDown preserves semantic structure and handles complex data, such as tables, with precision. This ensures high retrieval quality and enhances LLMs' ability to extract insights from diverse content types.
⚠️Note: MarkItDown does not interpret the content of images embedded in PowerPoint files. Instead, it extracts the images as-is, leaving their semantic meaning inaccessible to LLMs.
For example, an object in the slide would be processed like this:
![object #](object#.jpg)
Installation is straightforward:
# %pip install markitdown
Extracting Text from PPTX Using MarkItDown
In this section, we'll use MarkItDown to:
Convert PowerPoint slides to markdown format
Preserve the semantic structure and visual formatting
Maintain slide numbers and titles
Generate clean, readable text output
First, we need to initialize MarkItDown and run convert function to load the .pptx from local.
from markitdown import MarkItDownmd =MarkItDown()result = md.convert("data/07-ppt-loader-sample.pptx")result_text = result.text_contentprint(result_text[:500])
![object 2](object2.jpg)
# Natural Language Processing with Deep Learning
CS224N/Ling284
Christopher Manning
Lecture 2: Word Vectors, Word Senses, and Neural Classifiers
# Lecture Plan
10
Lecture 2: Word Vectors, Word Senses, and Neural Network Classifiers
Course organization (3 mins)
Optimization basics (5 mins)
Review of word2vec and looking at word vectors (12 mins)
More on word2vec (8 mins)
Can we capture the essence of word meaning more ef
Convert markdown format to Langchain Document format
The code below processes PowerPoint slides by splitting them into individual Document objects.
Each slide is converted into a Langchain Document object with metadata including the slide number and title.
from langchain_core.documents import Documentimport re# Initialize document processing for PowerPoint slides# Format: <!-- Slide number: X --> where X is the slide number# Split the input text into individual slides using HTML comment markersslides = re.split(r'<!--\s*Slide number:\s*(\d+)\s*-->', result_text)# Initialize list to store Document objectsdocuments = []# Process each slide:# - Start from index 1 since slides[0] is empty from the initial split# - Step by 2 because the split result alternates between:# 1. slide number (odd indices)# 2. slide content (even indices)# Example: ['', '1', 'content1', '2', 'content2', '3', 'content3']for i inrange(1, len(slides), 2):# Extract slide number and content slide_number = slides[i] content = slides[i +1].strip()if i +1<len(slides)else""# Extract slide title from first markdown header if present title_match = re.search(r'#\s*(.+?)(?=\n|$)', content) title = title_match.group(1).strip()if title_match else""# Create Document object with slide metadata doc =Document( page_content=content, metadata={"source": "data/07-ppt-loader-sample.pptx","slide_number": int(slide_number),"slide_title": title } ) documents.append(doc)documents[:2]
[Document(metadata={'source': '../99-TEMPLATE/assets/sample.pptx', 'slide_number': 1, 'slide_title': 'Natural Language Processing with Deep Learning'}, page_content='![object 2](object2.jpg)\n# Natural Language Processing with Deep Learning\nCS224N/Ling284\nChristopher Manning\nLecture 2: Word Vectors, Word Senses, and Neural Classifiers'),
Document(metadata={'source': '../99-TEMPLATE/assets/sample.pptx', 'slide_number': 2, 'slide_title': 'Lecture Plan'}, page_content='# Lecture Plan\n10\nLecture 2: Word Vectors, Word Senses, and Neural Network Classifiers\nCourse organization (3 mins)\nOptimization basics (5 mins)\nReview of word2vec and looking at word vectors (12 mins)\nMore on word2vec (8 mins)\nCan we capture the essence of word meaning more effectively by counting? (12m)\nEvaluating word vectors (10 mins)\nWord senses (10 mins)\nReview of classification and how neural nets differ (10 mins)\nIntroducing neural networks (10 mins)\n\nKey Goal: To be able to read and understand word embeddings papers by the end of class')]
MarkItDown efficiently handles tables in PowerPoint slides by converting them into clean Markdown table syntax.
This makes tabular data easily accessible for LLMs while preserving the original structure and formatting.