Integrate MarkItDown into your Python applications for programmatic document conversion.
1
Import and initialize
from markitdown import MarkItDown# Initialize with default settingsmd = MarkItDown()
2
Convert a file
# Convert a local fileresult = md.convert("document.pdf")# Access the Markdown contentprint(result.text_content)# Access the document title (if available)if result.title: print(f"Title: {result.title}")
3
Save the output
# Write to a filewith open("output.md", "w", encoding="utf-8") as f: f.write(result.markdown)
Enhance image and PowerPoint conversions with AI-generated descriptions:
from markitdown import MarkItDownfrom openai import OpenAIclient = OpenAI()md = MarkItDown( llm_client=client, llm_model="gpt-4o", llm_prompt="Describe this image in detail for a technical audience.")result = md.convert("presentation.pptx")print(result.text_content)
from markitdown import MarkItDown, FileConversionException, UnsupportedFormatExceptionmd = MarkItDown()try: result = md.convert("document.xyz") print(result.text_content)except UnsupportedFormatException: print("This file format is not supported")except FileConversionException as e: print(f"Conversion failed: {e}")