Skip to content

markmatsu/pdf-batch-translator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PDF Batch Translator / 英文PDF一括翻訳ツール

Batch-translate English PDFs into Japanese Markdown — body text only, structure preserved.

英文PDFを一括で放り込むと、本文だけを逐語訳して .md で出力する CLI ツール。

📝 Background & design notes / 解説記事: Building an AI PDF translation tool


What it does

Point it at a folder of English PDFs and it translates each one to Japanese, writing <folder>/jp-translations/<name>.md. Three things make it more than a folder loop over an API call:

  • Body text only. Navigation, search boxes, ads, prev/next links, page numbers, headers/footers, "Downloaded from…" lines, footnotes/endnotes, bibliographies, and image credits are stripped. Lines that repeat across pages are removed mechanically; the final body-vs-furniture judgment is left to the model.
  • Literal, structure-preserving. Translates faithfully (直訳) — word order, paragraphs, and headings are kept as Markdown — instead of loosely paraphrasing.
  • Context carried across chunks. The tail of the previous translation is passed back in on every call, so terminology and sentence flow stay consistent across chapters instead of each chunk being translated in isolation.

It also writes output atomically after every chunk, so Ctrl+C never leaves a corrupted file — the translation so far is safely on disk.

Requirements

  • Python 3.9+
  • An Anthropic API key
  • pip install -r requirements.txt (PyMuPDF, anthropic, python-dotenv)

Quick start

cd pdf-batch-translator
python3 -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate
pip install -r requirements.txt

export ANTHROPIC_API_KEY=sk-...     # or put it in a .env file (see below)

# batch: translate every PDF in a folder
python translate_pdfs.py /path/to/folder

# single file (handy for testing)
python translate_pdfs.py --file sample.pdf --out result.md

The API key can come from an environment variable or a .env file placed next to the script (or in the directory you run it from):

# .env
ANTHROPIC_API_KEY=sk-...

Options

Option Default Description
--file / -f Translate a single PDF (single-file / test mode)
--out / -o Output .md for single-file mode; defaults to <name>.md beside the source
--model claude-sonnet-4-6 Model to use; bump to claude-opus-4-8 etc. for higher quality
--chunk-chars 3500 Max source characters per API call
--context-chars 1200 Trailing chars of prior translation passed as context
--glossary Glossary file, one source = 訳語 pair per line
--max-chunk 10 Max chunks translated per file (safety cap); excess is skipped
--no-max-chunk off Remove the chunk cap and translate the whole file
--overwrite off Re-translate even if an output .md already exists (batch mode)

日本語ドキュメント

セットアップ(venv)

cd pdf-batch-translator
python3 -m venv .venv
source .venv/bin/activate          # Windows: .venv\Scripts\activate
pip install -r requirements.txt

APIキーは環境変数か .env のどちらでも可。.env を使う場合はスクリプトと 同じ場所(またはコマンドを実行するカレントディレクトリ)に置く:

# .env
ANTHROPIC_API_KEY=sk-...

環境変数で渡す場合:

export ANTHROPIC_API_KEY=sk-...   # Windows(PowerShell): $env:ANTHROPIC_API_KEY="sk-..."

使い方

フォルダ一括モード

python translate_pdfs.py /path/to/フォルダ

/path/to/フォルダ 内の *.pdf を順に処理し、 /path/to/フォルダ/jp-translations/<元ファイル名>.md に書き出す。

単一ファイルモード(テスト向け)

python translate_pdfs.py --file sample.pdf --out result.md

--out を省略すると、元PDFと同じ場所に <元ファイル名>.md を作成する。

用語集の例(任意)

Bank of England = イングランド銀行
Committee of Inspection = 監査委員会
clerk = 書記

動作の要点

  • 本文のみ:ナビゲーション/検索ボックス/広告/前後リンク/ページ番号/ヘッダ・フッタ/ ダウンロード表示/脚注・尾注/参考文献一覧/画像クレジットは訳出しない。 ページをまたいで繰り返す行は機械的に除去し、最終的な本文判定はモデルが行う。
  • 逐語訳:意訳しない。段落・見出し構造は保持。
  • 流れの考慮:直前までの訳文の末尾を毎回モデルに渡すので、章をまたいでも 訳語と文の流れが一貫する(単純に逐次投げるだけ、にしていない)。

再実行とスキップ(一括モード)

  • 出力ファイルが 存在しなければ翻訳し、存在すれば翻訳しない。 つまり、まだ訳していないファイルだけが処理される。
  • やり直したいときは、その出力ファイルを削除してから再実行する。 削除すれば次回の実行でそのファイルだけが翻訳される。

途中で止めたとき(Ctrl+C)

  • 訳はチャンクごとに .md へ上書き保存される。Ctrl+C で止めても、 直前のチャンクまでの訳が .md に残る(書き込みはアトミックなので壊れない)。
  • ただし、その .md は「存在する」ため、そのまま再実行してもスキップされ、 続きは翻訳されない。続きを訳すには、当該ファイルを削除してから再実行する。

チャンク数の上限(max_chunk)

  • 既定では 1 ファイルあたり 10 チャンクまでしか翻訳しない(--max-chunk 10)。 巨大な PDF で API を使いすぎないための安全弁。
  • 上限が適用されたファイルは、出力名が <名前>.upto10chunk.md になり、 訳文末尾に「上限が適用されたこと」と「解除方法」の注記が付く。
  • 全文を翻訳するには、その .upto10chunk.md を削除してから、
    • より大きい値を指定: --max-chunk 50 など、または
    • 上限を解除: --no-max-chunk を付けて再実行する。

Releases

No releases published

Packages

 
 
 

Contributors

Languages