社内文書をLLMに読ませたいとき、WordやExcelのファイルをそのまま渡せるわけではない。手作業でコピーして貼り付けると、見出しの階層や表の構造が崩れてしまう。markitdownは、Office文書やPDFなど各種ファイル形式をLLMが読みやすいMarkdownへ変換するMicrosoft製のツールである。実際にインストールし、ネットワークを遮断した状態で変換が動くかを確かめた。
markitdownはWord・Excel・PowerPoint・PDF・HTML等、多様なファイル形式を構造を保ったままMarkdownへ変換するツールである。ソースはMIT Licenseで公開されている(*1)。
1. インストールする
対応形式をすべて使えるようにする[all]エクストラでインストールする。テスト用のWord文書を作るためだけにpython-docxも入れる(markitdown自体の依存ではない)。
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'markitdown[all]==0.1.6' 'python-docx==1.2.0'
WORKDIR /work
podman build -t markitdown-sandbox .
2. Word文書をMarkdownへ変換する
見出し・段落・表を含むダミーのWord文書を作り、そのままmarkitdownに渡した。ネットワークを遮断した状態で実行する。
# make_docx.py
from docx import Document
doc = Document()
doc.add_heading("Quarterly Report", level=1)
doc.add_paragraph("This report summarizes rootless container adoption across three teams.")
doc.add_heading("Findings", level=2)
doc.add_paragraph("Teams that switched to rootless Podman reported fewer permission issues.")
table = doc.add_table(rows=3, cols=2)
table.rows[0].cells[0].text = "Team"
table.rows[0].cells[1].text = "Adoption"
table.rows[1].cells[0].text = "Platform"
table.rows[1].cells[1].text = "100%"
table.rows[2].cells[0].text = "Infra"
table.rows[2].cells[1].text = "60%"
doc.save("/work/report.docx")
# convert_demo.py
from markitdown import MarkItDown
md = MarkItDown()
result = md.convert("/work/report.docx")
print(result.text_content)
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work markitdown-sandbox \
bash -c "python make_docx.py && python convert_demo.py"
-vは指定したディレクトリをコンテナから読み書き可能にする。実験専用の空ディレクトリで実行し、未公開の文書やその他の秘密情報を置かないこと。
結果は次の通りだった。
# Quarterly Report
This report summarizes rootless container adoption across three teams.
## Findings
Teams that switched to rootless Podman reported fewer permission issues.
| | |
| --- | --- |
| Team | Adoption |
| Platform | 100% |
| Infra | 60% |
見出しの階層(#/##)と段落はそのまま保たれた。表もMarkdownのテーブル記法に変換されたが、1行目(「Team」「Adoption」という本来の見出し行)がヘッダーとして扱われず、空の見出し行が別途生成されている。実際にこのツールを使う際は、変換後の表の見出し行が意図通りになっているか、実データで確認しておいた方がよい。
文書の構造を、コピペで壊さない
見出しや表を含む文書をLLMに渡す作業は、コピー&ペーストでは構造が保たれない。markitdownのような変換ツールを間に挟めば、見出しの階層や表の区切りをMarkdownとして保ったままLLMに渡せる。ただし細部の変換結果は形式によって癖が出ることもあるため、渡す前に一度出力を見ておく習慣は残しておきたい。
出典
*1 markitdown (GitHub), https://github.com/microsoft/markitdown, MIT License