外掛系統

MarkItDown 用 Python entry-point 讓第三方擴充「安裝即註冊」,預設關閉

運作方式

  1. 外掛套件在 pyproject.toml 宣告 entry-point:[project.entry-points."markitdown.plugin"]
  2. 套件匯出 __plugin_interface_version__register_converters(markitdown, **kwargs)
  3. 每次建立 MarkItDown 實例時,若 enable_plugins=True(或 CLI --use-plugins), 會 entry_points(group="markitdown.plugin") 載入全部已安裝外掛,呼叫 register_converters()
  4. 外掛可用 register_converter(converter, priority=...) 在任何優先級插入 converter——包括搶在內建 converter 之前。

外掛預設關閉

# 列出已安裝外掛
markitdown --list-plugins

# 使用外掛
markitdown --use-plugins path-to-file.pdf

# Python API
md = MarkItDown(enable_plugins=True)
result = md.convert("path-to-file.rtf")
注意:外掛載入失敗(entry_point.load() 拋例外)不會中斷, 只會 warn() 並跳過——所以裝了外掛但沒生效時,先檢查 --list-plugins

官方外掛:markitdown-ocr

LLM Vision 抽取 PDF、DOCX、PPTX、XLSX 中「內嵌圖片」的文字(OCR), 不需要新 ML 函式庫——沿用核心既有的 llm_client / llm_model 模式。

pip install markitdown-ocr
pip install openai

markitdown document.pdf --use-plugins --llm-client openai --llm-model gpt-4o

重點行為:

自己寫外掛:markitdown-sample-plugin 範本

以 RtfConverter 為例,三步完成:

# 1. 實作 DocumentConverter
class RtfConverter(DocumentConverter):
    def accepts(self, file_stream, stream_info, **kwargs) -> bool:
        ...  # 判斷是不是 RTF
    def convert(self, file_stream, stream_info, **kwargs) -> DocumentConverterResult:
        ...  # 轉成 Markdown

# 2. 匯出外掛協定
__plugin_interface_version__ = 1

def register_converters(markitdown: MarkItDown, **kwargs):
    markitdown.register_converter(RtfConverter())

# 3. pyproject.toml 宣告 entry-point
# [project.entry-points."markitdown.plugin"]
# sample_plugin = "markitdown_sample_plugin"

完整註解範例見 程式碼對照 · OCR / 外掛套件

找外掛:在 GitHub 搜尋 #markitdown-plugin 標籤可找到社群外掛。