MarkItDown 是微软开源的一个 Python 库和命令行工具,专门用于将各种格式的文档转换为 Markdown。它的核心理念很简单:无论你的数据是 PDF、Word 文档、Excel 表格、图片还是音频文件,MarkItDown 都能将其转换成 LLM 最友好的格式——Markdown。
为什么需要 MarkItDown?
在构建 RAG 应用或为 LLM 准备数据时,最大的挑战之一就是处理各种非结构化的文档格式。PDF 的文本提取可能乱码,Word 文档的表格可能丢失,图片中的文字无法直接读取。MarkItDown 就像一个"万能翻译器",把这些五花八门的格式统一转换成干净、结构化的 Markdown。
与 Pandoc 这类追求高保真排版的工具不同,MarkItDown 的目标非常明确:为 LLM 准备高质量、token 高效的输入数据。
安装
安装 MarkItDown 最简单的方式是使用 pip,推荐安装所有可选依赖以支持全部功能:
pip install 'markitdown[all]'
如果你只需要特定格式的支持,可以按需安装:
# 仅安装 PDF、PowerPoint、Word 支持pip install 'markitdown[pdf,pptx,docx]'# 仅安装图片和音频支持pip install 'markitdown[ocr,audio]'
验证安装是否成功:
基础用法:命令行
MarkItDown 提供了简洁的 CLI 接口,最基础的使用方式如下:
# 转换 PDF 并输出到终端markitdown document.pdf# 保存到文件markitdown document.pdf -o output.md# 处理 Word 文档markitdown report.docx -o report.md# 处理 Excel 表格markitdown data.xlsx -o data.md# 使用管道重定向cat document.pdf | markitdown > output.md
当你需要处理没有扩展名的文件或从标准输入读取内容时,可以使用 -x 参数指定文件类型:
# 将 HTML 内容作为文本文件转换cat webpage.html | markitdown -x html
基础用法:Python API
MarkItDown 的 Python API 同样简洁明了。以下是一个最简单的转换示例:
from markitdown import MarkItDown# 创建转换器实例md = MarkItDown()# 转换 Excel 文件result = md.convert("test.xlsx")# 输出 Markdown 内容print(result.text_content)
result.text_content 包含了转换后的完整 Markdown 文本。对于表格数据,MarkItDown 会自动将其转换为 Markdown 表格格式:
from markitdown import MarkItDownmd = MarkItDown()result = md.convert("employees.csv")print(result.text_content)# 输出类似:# | First Name | Last Name | Department | Position |# |------------|-----------|------------|----------|# | Alice | Johnson | Marketing | Coordinator |# | Bob | Williams | HR | Generalist |
核心功能:图片描述与 OCR
MarkItDown 支持从图片中提取文字。默认情况下,它会提取 EXIF 元数据。但如果想要更智能的图片描述——比如让 LLM 生成图片内容的文字描述,你可以配置 LLM 客户端:
from markitdown import MarkItDownfrom openai import OpenAI# 初始化 OpenAI 客户端client = OpenAI(api_key="your-api-key")# 创建支持 LLM 图片描述的转换器md = MarkItDown( llm_client=client, llm_model="gpt-4o" # 推荐使用支持视觉的模型)# 转换图片result = md.convert("example.jpg")print(result.text_content)# 输出将包含 GPT-4o 生成的图片描述
这个功能对于处理大量图片文件非常有用——你不仅获得了图片中的 OCR 文本,还能得到 AI 生成的图片内容描述。
核心功能:音频转录
MarkItDown 还可以将音频文件中的语音转录为文字。安装时确保包含了音频支持:
pip install 'markitdown[audio]'
在 Python 中使用:
from markitdown import MarkItDownmd = MarkItDown()result = md.convert("meeting.mp3")print(result.text_content) # 输出会议录音的转录文本
进阶功能:批量处理多个文件
在实际项目中,你经常需要处理整个文件夹的文档。以下是一个批量转换脚本:
from markitdown import MarkItDownimport osfrom pathlib import Pathdef batch_convert(input_dir, output_dir): """ 批量转换指定目录下的所有支持文件 """ md = MarkItDown() # 支持的文件扩展名 supported_extensions = ('.pdf', '.docx', '.xlsx', '.pptx', '.jpg', '.png') # 创建输出目录 Path(output_dir).mkdir(parents=True, exist_ok=True) converted_count = 0 failed_files = [] for file in os.listdir(input_dir): if file.lower().endswith(supported_extensions): input_path = os.path.join(input_dir, file) output_path = os.path.join( output_dir, f"{Path(file).stem}.md" ) print(f"正在转换: {file}") try: result = md.convert(input_path) with open(output_path, 'w', encoding='utf-8') as f: f.write(result.text_content) converted_count += 1 print(f"✓ 已保存到: {output_path}") except Exception as e: failed_files.append((file, str(e))) print(f"✗ 转换失败: {e}") print(f"\n转换完成! 成功: {converted_count}, 失败: {len(failed_files)}") if failed_files: print("\n失败的文件:") for file, error in failed_files: print(f" - {file}: {error}")# 使用示例batch_convert("./documents", "./markdown_outputs")
进阶功能:与 Azure Document Intelligence 集成
如果你需要处理复杂的 PDF 或扫描件,可以使用 Azure Document Intelligence 服务来获得更好的 OCR 效果:
from markitdown import MarkItDown# 使用 Azure Document Intelligence 增强转换md = MarkItDown( docintel_endpoint="<your_endpoint>", docintel_key="<your_api_key>")result = md.convert("scanned_document.pdf")print(result.text_content)
Azure Document Intelligence 能更好地处理复杂的文档布局、表格和手写文字。
进阶功能:处理 ZIP 压缩包
MarkItDown 支持直接处理 ZIP 文件,它会自动遍历压缩包内的所有文件并转换:
from markitdown import MarkItDownmd = MarkItDown()result = md.convert("archive.zip")# result.text_content 将包含所有内部文件的转换结果print(result.text_content)
这对于批量处理打包好的文档非常方便。
与 MCP 服务器集成
MarkItDown 还提供了 MCP (Model Context Protocol) 服务器版本,可以直接与 Claude Desktop 等应用集成。安装方式:
pip install markitdown-mcp-advanced
配置 Claude Desktop 后,你就可以在对话中直接让 Claude 帮你转换文档了。
支持的格式一览
MarkItDown 支持以下格式的转换:
PDF (.pdf) - 使用 pdfminer.six 进行文本提取
PowerPoint (.pptx) - 提取幻灯片中的文本和表格
Word (.docx) - 使用 python-docx 和 mammoth 处理
Excel (.xlsx) - 使用 pandas 读取并转换为 Markdown 表格
图片 (.jpg, .png, .gif, .bmp, .tiff, .webp) - 支持 EXIF 元数据和 OCR 文字提取
音频 (.mp3, .wav, .m4a, .flac) - 支持 EXIF 元数据和语音转录
HTML (.html) - 使用 BeautifulSoup 解析并转换为 Markdown
文本格式 (CSV, JSON, XML) - 自动格式化为 Markdown 表格或代码块
ZIP 文件 - 遍历压缩包内的所有文件
实际应用场景
场景一:构建 RAG 知识库
from markitdown import MarkItDownimport chromadb # 假设使用 ChromaDBdef prepare_documents_for_rag(file_list): """ 将多个文档转换为 Markdown 并准备向量化 """ md = MarkItDown() documents = [] for file_path in file_list: result = md.convert(file_path) documents.append({ "source": file_path, "content": result.text_content }) # 这里可以将 documents 存入向量数据库 return documents# 使用示例docs = prepare_documents_for_rag([ "company_policy.pdf", "product_manual.docx", "financial_report.xlsx"])
场景二:网页内容提取与处理
from markitdown import MarkItDownimport requestsdef convert_webpage_to_markdown(url): """ 下载网页并转换为 Markdown """ # 下载网页内容 response = requests.get(url) html_content = response.text # 将 HTML 内容保存为临时文件或直接处理 with open("temp.html", "w", encoding="utf-8") as f: f.write(html_content) # 使用 MarkItDown 转换 md = MarkItDown() result = md.convert("temp.html") return result.text_content# 使用示例markdown = convert_webpage_to_markdown("https://example.com/article")print(markdown[:500]) # 输出前500个字符
场景三:自动化文档处理流水线
from markitdown import MarkItDownfrom pathlib import Pathimport jsonimport logginglogging.basicConfig(level=logging.INFO)logger = logging.getLogger(__name__)class DocumentProcessor: """ 文档处理流水线类 """ def __init__(self, input_dir, output_dir): self.input_dir = Path(input_dir) self.output_dir = Path(output_dir) self.converter = MarkItDown() self.stats = {"processed": 0, "failed": 0} def process_all(self): """处理目录下的所有文档""" self.output_dir.mkdir(parents=True, exist_ok=True) for file_path in self.input_dir.iterdir(): if file_path.is_file(): self.process_single(file_path) self.save_stats() return self.stats def process_single(self, file_path): """处理单个文件""" try: logger.info(f"处理: {file_path.name}") result = self.converter.convert(str(file_path)) output_file = self.output_dir / f"{file_path.stem}.md" with open(output_file, 'w', encoding='utf-8') as f: f.write(result.text_content) self.stats["processed"] += 1 logger.info(f"完成: {output_file}") except Exception as e: self.stats["failed"] += 1 logger.error(f"失败 {file_path.name}: {e}") def save_stats(self): """保存处理统计信息""" stats_file = self.output_dir / "processing_stats.json" with open(stats_file, 'w') as f: json.dump(self.stats, f, indent=2)# 使用示例processor = DocumentProcessor("./raw_docs", "./processed_markdown")stats = processor.process_all()print(f"处理完成: 成功 {stats['processed']} 个, 失败 {stats['failed']} 个")
注意事项与限制
虽然 MarkItDown 非常强大,但也有一些需要注意的地方:
不是高保真转换工具:MarkItDown 的目标是提取可用的文本内容,而非完美还原文档的排版和视觉效果。如果需要精确的布局还原,Pandoc 可能是更好的选择。
依赖第三方库:MarkItDown 本质上是多个现有库的封装,如 python-docx、pdfminer.six、pandas 等。这意味着它的功能上限受限于这些底层库。
复杂 PDF 可能效果不佳:对于复杂的多列布局、扫描件 PDF,可能需要结合 Azure Document Intelligence 等专业服务。
图片描述需要 API 密钥:使用 LLM 生成图片描述需要 OpenAI API 密钥或类似的 LLM 服务。
总结
MarkItDown 是微软开源的一个实用工具,专门解决一个明确的问题:将各种文档格式快速、干净地转换为 LLM 友好的 Markdown。它的 API 简洁,易于集成,支持从简单的命令行操作到复杂的批量处理流水线。
无论你是在构建 RAG 应用、准备训练数据,还是只是想把一堆不同格式的文档整理成统一的 Markdown 格式,MarkItDown 都值得一试。它的最大价值在于让你专注于业务逻辑,而不是被各种文档解析的细节所困扰。