The Translator in Your Pocket: Translate Any PDF Book With Python

 


You have a PDF book in a foreign language, but you keep reaching for the dictionary every few pages. Or maybe there's a book you've wanted to read for years — and it was never translated. That's exactly why I wrote this guide.

The method I describe here is completely free. No app to purchase, no subscription required. Every tool we use is open source and runs on Google's free translation infrastructure. I tested this on my Android device — it worked. I adapted the same approach for all other platforms and tested each one. Windows is included too, so no one gets left behind.

It might look a bit technical, especially if you've never used a command line before. But we go through it step by step, and I explain what each command does. You don't need to understand the code — just copy, paste, and follow along. Pick your platform from the sections below and get started.

💸
Completely FreeNo subscription, no purchase required
📱
4 PlatformsiOS, macOS, Android and Windows
🌍
Any LanguageWorks with PDFs in languages other than English too
📱

iOS — iSH Shell

Translate PDFs on your iPhone using an Alpine Linux environment

⬇ DOWNLOAD THE APP FIRST

iSH Shell is available for free on the App Store.
Search for "iSH Shell" or go directly to:
apps.apple.com → iSH Shell

Open the app — a terminal screen will appear. You're ready to go.

⚠ BEFORE YOU START — IMPORTANT NOTES
  • iSH is based on Alpine Linux — use apk, not apt or pkg
  • Because the iPhone uses an ARM processor, iSH emulates x86 — downloads can take a very long time, be patient
  • Your book file must be placed in the iSH → root folder inside the Files app
  • Keep the filename short and simple — example: book.pdf
  • To save in nano: ^ then O → Enter  |  To exit: ^ then X
01

Update iSH

Updates the package list and installed packages

SHELL
apk update && apk upgrade
02

Install Python and pip

Installs the Python interpreter and package manager

SHELL
apk add python3 py3-pip
03

Verify Python Version

Confirms the installation was successful

SHELL
python3 --version
04

Install System Libraries

C libraries required to compile Reportlab and Pillow

SHELL
apk add zlib-dev jpeg-dev gcc musl-dev
apk add python3-dev
Note: The first line installs C libraries for Reportlab; the second installs Python headers required to compile Pillow.
05

Install Python Packages

The three essential libraries for translation

SHELL
pip3 install PyPDF2 deep-translator reportlab
PyPDF2 → reads PDFs  |  deep-translator → Google Translate API  |  reportlab → generates PDFs
Warning: Pillow compilation can take 20–30 minutes. Do not touch the screen — just wait.
06

Install Font

DejaVu font for proper character rendering in the output PDF

SHELL
apk add ttf-dejavu
07

Create the Translation Script

Reads the PDF, translates it, and saves the result as TXT

SHELL
nano /root/translate.py
— PASTE THE CODE BELOW —
PYTHON
import PyPDF2
from deep_translator import GoogleTranslator
import time

INPUT  = "/root/book.pdf"              # Path to your PDF file
OUTPUT = "/root/translated_raw.txt"    # Where the translation will be saved

def translate():
    text = ""
    with open(INPUT, "rb") as f:
        reader = PyPDF2.PdfReader(f)
        total = len(reader.pages)
        for i, page in enumerate(reader.pages):
            text += page.extract_text() or ""
            print(f"Reading: {i+1}/{total}")

    chunks = [text[i:i+4000] for i in range(0, len(text), 4000)]
    # Splits text into 4000-character chunks (Google Translate limit is 5000)
    total = len(chunks)

    with open(OUTPUT, "w", encoding="utf-8") as f:
        for i, chunk in enumerate(chunks):
            try:
                result = GoogleTranslator(source="en", target="tr").translate(chunk)
                f.write(result)
                f.flush()                    # Writes each chunk to disk immediately
                print(f"Translating: {i+1}/{total}")
                time.sleep(1)              # 1s delay to avoid Google rate limiting
            except Exception as e:
                print(f"Error {i+1}: {e}")
                time.sleep(5)              # On error, wait 5s and continue

    print(f"Done! {OUTPUT}")

translate()
source="en" → If your PDF is not in English, change this value. If unsure, use source="auto" — Google will detect it automatically. Examples: French → "fr", German → "de", Spanish → "es".
— SAVE AND EXIT —
NANO
# Save:  ^ + O  →  Enter
# Exit:   ^ + X
— RUN —
SHELL
python3 /root/translate.py
Tip: If you see "Reading: 1/..." on screen, the script found your file and is running.
08

Create the PDF Builder Script

Converts the raw translated TXT into a formatted PDF

SHELL
nano /root/make_pdf.py
— PASTE THE CODE BELOW —
PYTHON
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4

INPUT  = "/root/translated_raw.txt"
OUTPUT = "/root/translated.pdf"
FONT   = "/usr/share/fonts/ttf-dejavu/DejaVuSans.ttf"
# NOTE: If font path fails, run: find / -name "DejaVuSans.ttf" 2>/dev/null

pdfmetrics.registerFont(TTFont("DejaVu", FONT))

with open(INPUT, "r", encoding="utf-8") as f:
    lines = f.readlines()

c = canvas.Canvas(OUTPUT, pagesize=A4)
width, height = A4
c.setFont("DejaVu", 11)
y = height - 50
left = 50
max_width = width - 100

def wrap_line(c, text, max_w):
    words = text.split()
    line = ""
    wrapped = []
    for word in words:
        test = line + " " + word if line else word
        if c.stringWidth(test, "DejaVu", 11) < max_w:
            line = test
        else:
            if line: wrapped.append(line)
            line = word
    if line: wrapped.append(line)
    return wrapped

for line in lines:
    line = line.strip()
    if line:
        wrapped = wrap_line(c, line, max_width)
        for w in wrapped:
            if y < 50:
                c.showPage()
                c.setFont("DejaVu", 11)
                y = height - 50
            c.drawString(left, y, w)
            y -= 16
    else:
        y -= 8

c.save()
print("Done! translated.pdf has been saved.")
— SAVE AND EXIT —
NANO
# Save:  ^ + O  →  Enter
# Exit:   ^ + X
— RUN —
SHELL
python3 /root/make_pdf.py

✓ OUTPUT FILES

📄
/root/translated_raw.txt
Raw translated text
📕
/root/translated.pdf
Final translated PDF — A4 format
💻

macOS — Terminal

Translate PDFs on your Mac using the built-in Terminal app

⬇ WHERE IS TERMINAL?

Terminal comes pre-installed on macOS — nothing to download.

To open: press ⌘ + Space → type "Terminal" → press Enter.
Or: Finder → Applications → Utilities → Terminal.

⚠ BEFORE YOU START — IMPORTANT NOTES
  • Python 3 usually comes pre-installed on macOS — check by running Step 1
  • If you get a "python3 not found" error, download Python 3 from python.org
  • Place your book on the Desktop with a short, simple name — example: book.pdf
  • Update the INPUT line in the script with your exact filename
  • To save in nano: Ctrl + O → Enter  |  To exit: Ctrl + X
01

Check Python Version

Verifies that Python is installed

SHELL
python3 --version
02

Install Required Libraries

The three essential Python packages for translation

SHELL
pip3 install PyPDF2 deep-translator reportlab
PyPDF2 → reads PDFs  |  deep-translator → Google Translate API  |  reportlab → generates PDFs
03

Create the Translation Script

Reads the PDF, translates it, and saves the result as TXT

SHELL
nano ~/Desktop/translate.py
— PASTE THE CODE BELOW —
PYTHON
import PyPDF2
from deep_translator import GoogleTranslator
import time, os

INPUT  = os.path.expanduser("~/Desktop/book.pdf")
OUTPUT = os.path.expanduser("~/Desktop/translated_raw.txt")

def translate():
    text = ""
    with open(INPUT, "rb") as f:
        reader = PyPDF2.PdfReader(f)
        total = len(reader.pages)
        for i, page in enumerate(reader.pages):
            text += page.extract_text() or ""
            print(f"Reading: {i+1}/{total}")
    chunks = [text[i:i+4000] for i in range(0, len(text), 4000)]
    total = len(chunks)
    with open(OUTPUT, "w", encoding="utf-8") as f:
        for i, chunk in enumerate(chunks):
            try:
                result = GoogleTranslator(source="en", target="tr").translate(chunk)
                f.write(result); f.flush()
                print(f"Translating: {i+1}/{total}")
                time.sleep(1)
            except Exception as e:
                print(f"Error {i+1}: {e}"); time.sleep(5)
    print(f"Done! {OUTPUT}")

translate()
source="en" → If your PDF is not in English, change this value. If unsure, use source="auto". Examples: French → "fr", German → "de", Spanish → "es".
— SAVE AND EXIT —
NANO
# Save:  Ctrl + O  →  Enter
# Exit:   Ctrl + X
— RUN —
SHELL
python3 ~/Desktop/translate.py
Tip: If you see "Reading: 1/..." on screen, the script found your file and is running.
04

Create the PDF Builder Script

Converts the raw translated TXT into a formatted PDF

SHELL
nano ~/Desktop/make_pdf.py
— PASTE THE CODE BELOW —
PYTHON
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4
import os

INPUT  = os.path.expanduser("~/Desktop/translated_raw.txt")
OUTPUT = os.path.expanduser("~/Desktop/translated.pdf")
FONT   = "/System/Library/Fonts/Supplemental/Arial.ttf"
# NOTE: If this path fails, run: find /System/Library/Fonts -name "*.ttf" | head -10

pdfmetrics.registerFont(TTFont("Arial", FONT))

with open(INPUT, "r", encoding="utf-8") as f:
    lines = f.readlines()

c = canvas.Canvas(OUTPUT, pagesize=A4)
width, height = A4
c.setFont("Arial", 11)
y = height - 50; left = 50; max_width = width - 100

def wrap_line(c, text, max_w):
    words = text.split(); line = ""; wrapped = []
    for word in words:
        test = line + " " + word if line else word
        if c.stringWidth(test, "Arial", 11) < max_w: line = test
        else:
            if line: wrapped.append(line)
            line = word
    if line: wrapped.append(line)
    return wrapped

for line in lines:
    line = line.strip()
    if line:
        for w in wrap_line(c, line, max_width):
            if y < 50: c.showPage(); c.setFont("Arial", 11); y = height - 50
            c.drawString(left, y, w); y -= 16
    else: y -= 8

c.save()
print("Done! translated.pdf has been saved.")
— SAVE AND EXIT —
NANO
# Save:  Ctrl + O  →  Enter
# Exit:   Ctrl + X
— RUN —
SHELL
python3 ~/Desktop/make_pdf.py

✓ OUTPUT FILES

📄
~/Desktop/translated_raw.txt
Raw translated text — on your Desktop
📕
~/Desktop/translated.pdf
Final translated PDF — on your Desktop
🤖

Android — Termux

Translate PDFs on your Android device using the Termux terminal app

⬇ DOWNLOAD THE APP FIRST

You can get Termux from any of these sources:

🟢 F-Droid (recommended): f-droid.org → Termux
🟡 GitHub: github.com/termux/termux-app
🔴 Play Store: May be an older version — F-Droid is preferred.

Open the app — a terminal screen will appear. You're ready.

⚠ BEFORE YOU START — IMPORTANT NOTES
  • Run termux-setup-storage to grant storage access — without this your PDF won't be accessible
  • Place your PDF in the Downloads folder with a short name — example: book.pdf
  • Update the INPUT path in the script to match your actual file location
  • If curl is missing: pkg install curl
  • To save in nano: Ctrl + O → Enter  |  To exit: Ctrl + X
01

Update Termux

Updates package list and installed packages

SHELL
pkg update && pkg upgrade
02

Grant Storage Permission

Required for Termux to access your phone's files

SHELL
termux-setup-storage
Note: A permission dialog will appear — tap Allow. Without this, the script cannot read your PDF.
03

Install Python

Installs the Python interpreter

SHELL
pkg install python
04

Verify Python Version

Confirms the installation was successful

SHELL
python --version
05

Install Required Libraries

The three essential Python packages for translation

SHELL
pip install PyPDF2 deep-translator reportlab
PyPDF2 → reads PDFs  |  deep-translator → Google Translate API  |  reportlab → generates PDFs
06

Download Font

DejaVu font for proper character rendering in the output PDF

SHELL
curl -o ~/DejaVuSans.ttf https://github.com/dejavu-fonts/dejavu-fonts/raw/master/ttf/DejaVuSans.ttf
If curl is missing: Run pkg install curl first.
07

Create the Translation Script

Reads the PDF, translates it, and saves the result as TXT

SHELL
nano ~/translate.py
— PASTE THE CODE BELOW —
PYTHON
import PyPDF2
from deep_translator import GoogleTranslator
import time

INPUT  = "/storage/emulated/0/Download/book.pdf"
OUTPUT = "/storage/emulated/0/Download/translated_raw.txt"

def translate():
    text = ""
    with open(INPUT, "rb") as f:
        reader = PyPDF2.PdfReader(f)
        total = len(reader.pages)
        for i, page in enumerate(reader.pages):
            text += page.extract_text() or ""
            print(f"Reading: {i+1}/{total}")
    chunks = [text[i:i+4000] for i in range(0, len(text), 4000)]
    total = len(chunks)
    with open(OUTPUT, "w", encoding="utf-8") as f:
        for i, chunk in enumerate(chunks):
            try:
                result = GoogleTranslator(source="en", target="tr").translate(chunk)
                f.write(result); f.flush()
                print(f"Translating: {i+1}/{total}")
                time.sleep(1)
            except Exception as e:
                print(f"Error {i+1}: {e}"); time.sleep(5)
    print(f"Done! {OUTPUT}")

translate()
source="en" → If your PDF is not in English, change this. If unsure, use source="auto". Examples: French → "fr", German → "de", Spanish → "es".
— SAVE AND EXIT —
NANO
# Save:  Ctrl + O  →  Enter
# Exit:   Ctrl + X
— RUN —
SHELL
python ~/translate.py
Tip: If you see "Reading: 1/..." on screen, the script found your file and is running.
08

Create the PDF Builder Script

Converts the raw translated TXT into a formatted PDF

SHELL
nano ~/make_pdf.py
— PASTE THE CODE BELOW —
PYTHON
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4

INPUT  = "/storage/emulated/0/Download/translated_raw.txt"
OUTPUT = "/storage/emulated/0/Download/translated.pdf"
FONT   = "/data/data/com.termux/files/home/DejaVuSans.ttf"

pdfmetrics.registerFont(TTFont("DejaVu", FONT))

with open(INPUT, "r", encoding="utf-8") as f:
    lines = f.readlines()

c = canvas.Canvas(OUTPUT, pagesize=A4)
width, height = A4
c.setFont("DejaVu", 11)
y = height - 50; left = 50; max_width = width - 100

def wrap_line(c, text, max_w):
    words = text.split(); line = ""; wrapped = []
    for word in words:
        test = line + " " + word if line else word
        if c.stringWidth(test, "DejaVu", 11) < max_w: line = test
        else:
            if line: wrapped.append(line)
            line = word
    if line: wrapped.append(line)
    return wrapped

for line in lines:
    line = line.strip()
    if line:
        for w in wrap_line(c, line, max_width):
            if y < 50: c.showPage(); c.setFont("DejaVu", 11); y = height - 50
            c.drawString(left, y, w); y -= 16
    else: y -= 8

c.save()
print("Done! translated.pdf has been saved.")
— SAVE AND EXIT —
NANO
# Save:  Ctrl + O  →  Enter
# Exit:   Ctrl + X
— RUN —
SHELL
python ~/make_pdf.py

✓ OUTPUT FILES

📄
/storage/emulated/0/Download/translated_raw.txt
Raw translated text — in Downloads
📕
/storage/emulated/0/Download/translated.pdf
Final translated PDF — in Downloads
🪟

Windows — PowerShell & Notepad

Translate PDFs on Windows using PowerShell and Notepad

⬇ WHERE IS POWERSHELL?

PowerShell comes pre-installed on Windows 10 and 11 — nothing to download.

To open: type "PowerShell" in the Start menu → right-click → Run as Administrator.
Or press Windows + X → select Windows PowerShell (Admin).

⚠ BEFORE YOU START — IMPORTANT NOTES
  • During Python installation, check "Add Python to PATH" — if skipped, the python command won't work
  • Place your book on the Desktop with a short, simple name — example: book.pdf
  • Avoid spaces or special characters in the file path — they can cause errors
  • We'll use Notepad to create the scripts — saving instructions are below
  • Arial is pre-installed on Windows and supports a wide range of characters — no font download needed
01

Download and Install Python

Python must be installed separately on Windows

🔗 PYTHON DOWNLOAD

Go to python.org/downloads/windows.

Click Download Python 3.x.x → run the downloaded .exe file.

⚠ On the installer screen, check "Add Python to PATH" at the bottom before clicking Install Now.

02

Verify Python Version

Open PowerShell as Administrator and run this

POWERSHELL
python --version
Note: If you get a "python is not recognized" error, you likely skipped "Add to PATH". Uninstall Python and reinstall it — this time check that box.
03

Install Required Libraries

The three essential Python packages for translation

POWERSHELL
pip install PyPDF2 deep-translator reportlab
PyPDF2 → reads PDFs  |  deep-translator → Google Translate API  |  reportlab → generates PDFs
04

Create the Translation Script

Use Notepad to create translate.py

📝 CREATING A SCRIPT WITH NOTEPAD

1. Open Notepad from the Start menu
2. Copy and paste the code below
3. Click File → Save As
4. Change file type to "All Files (*.*)"
5. Set filename to translate.py
6. Choose Desktop as location → Save

— PASTE THE CODE BELOW —
PYTHON
import PyPDF2
from deep_translator import GoogleTranslator
import time, os

INPUT  = os.path.expanduser("~/Desktop/book.pdf")
OUTPUT = os.path.expanduser("~/Desktop/translated_raw.txt")

def translate():
    text = ""
    with open(INPUT, "rb") as f:
        reader = PyPDF2.PdfReader(f)
        total = len(reader.pages)
        for i, page in enumerate(reader.pages):
            text += page.extract_text() or ""
            print(f"Reading: {i+1}/{total}")
    chunks = [text[i:i+4000] for i in range(0, len(text), 4000)]
    total = len(chunks)
    with open(OUTPUT, "w", encoding="utf-8") as f:
        for i, chunk in enumerate(chunks):
            try:
                result = GoogleTranslator(source="en", target="tr").translate(chunk)
                f.write(result); f.flush()
                print(f"Translating: {i+1}/{total}")
                time.sleep(1)
            except Exception as e:
                print(f"Error {i+1}: {e}"); time.sleep(5)
    print(f"Done! {OUTPUT}")

translate()
source="en" → If your PDF is not in English, change this. If unsure, use source="auto". Examples: French → "fr", German → "de", Spanish → "es".
— RUN FROM POWERSHELL —
POWERSHELL
python "$env:USERPROFILE\Desktop\translate.py"
Tip: If you see "Reading: 1/..." on screen, the script found your file and is running.
05

Create the PDF Builder Script

Converts the raw translated TXT into a formatted PDF

📝 CREATING A SCRIPT WITH NOTEPAD

1. Open a new Notepad window
2. Copy and paste the code below
3. Click File → Save As
4. Change file type to "All Files (*.*)"
5. Set filename to make_pdf.py
6. Choose Desktop as location → Save

— PASTE THE CODE BELOW —
PYTHON
from reportlab.pdfgen import canvas
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A4
import os

INPUT  = os.path.expanduser("~/Desktop/translated_raw.txt")
OUTPUT = os.path.expanduser("~/Desktop/translated.pdf")
FONT   = "C:\\Windows\\Fonts\\arial.ttf"
# NOTE: Arial is pre-installed on Windows. No extra download needed.

pdfmetrics.registerFont(TTFont("Arial", FONT))

with open(INPUT, "r", encoding="utf-8") as f:
    lines = f.readlines()

c = canvas.Canvas(OUTPUT, pagesize=A4)
width, height = A4
c.setFont("Arial", 11)
y = height - 50; left = 50; max_width = width - 100

def wrap_line(c, text, max_w):
    words = text.split(); line = ""; wrapped = []
    for word in words:
        test = line + " " + word if line else word
        if c.stringWidth(test, "Arial", 11) < max_w: line = test
        else:
            if line: wrapped.append(line)
            line = word
    if line: wrapped.append(line)
    return wrapped

for line in lines:
    line = line.strip()
    if line:
        for w in wrap_line(c, line, max_width):
            if y < 50: c.showPage(); c.setFont("Arial", 11); y = height - 50
            c.drawString(left, y, w); y -= 16
    else: y -= 8

c.save()
print("Done! translated.pdf has been saved.")
— RUN FROM POWERSHELL —
POWERSHELL
python "$env:USERPROFILE\Desktop\make_pdf.py"

✓ OUTPUT FILES

📄
C:\Users\YourUsername\Desktop\translated_raw.txt
Raw translated text — on your Desktop
📕
C:\Users\YourUsername\Desktop\translated.pdf
Final translated PDF — on your Desktop


If you liked this article, you might also like my other work:


Comments