49 – Real-World Python Projects – Automated File Translator

This project lets you input any file (TXT, PDF, DOCX) → and it automatically translates it into any language using an AI API.

Perfect for:

✔ Students
✔ Freelancers
✔ Content creators
✔ Businesses translating documents


🚀 What This Project Supports

Accept File Types:

  • .txt
  • .pdf
  • .docx
  • .md
  • Images (optional with OCR)

Translates Into:

  • Any language supported by the API (English, Hindi, Tamil, Arabic, French, etc.)

Outputs:

  • New translated file
  • Auto-detected language
  • Translation summary
  • Error handling

🛠️ Python Libraries Needed

pip install googletrans==4.0.0-rc1
pip install python-docx
pip install PyPDF2
pip install deep-translator

📁 Folder Structure

file_translator/
│── translate.py
│── input_files/
│── output_files/

🧠 Core Translation Script: translate.py

from googletrans import Translator
from docx import Document
import PyPDF2
import os

translator = Translator()

def translate_text(text, dest_lang):
    result = translator.translate(text, dest=dest_lang)
    return result.text

def read_txt(path):
    with open(path, "r", encoding="utf-8") as f:
        return f.read()

def read_pdf(path):
    reader = PyPDF2.PdfReader(path)
    text = ""
    for page in reader.pages:
        text += page.extract_text() + "\n"
    return text

def read_docx(path):
    doc = Document(path)
    return "\n".join([para.text for para in doc.paragraphs])

def write_output(text, out_path):
    with open(out_path, "w", encoding="utf-8") as f:
        f.write(text)

def auto_translate(file_path, dest_lang):
    ext = file_path.split(".")[-1].lower()

    if ext == "txt":
        content = read_txt(file_path)
    elif ext == "pdf":
        content = read_pdf(file_path)
    elif ext == "docx":
        content = read_docx(file_path)
    else:
        return "Unsupported file type."

    translated = translate_text(content, dest_lang)

    out_path = "output_files/translated_" + os.path.basename(file_path) + ".txt"
    write_output(translated, out_path)

    return f"Translation complete! Saved to {out_path}"

# Example
print(auto_translate("input_files/sample.pdf", "hi"))

🔥 Features You Can Add

🔹 1. Auto Language Detection

detected = translator.detect(content)
print("Detected Language:", detected.lang)

🔹 2. Batch File Translation

Translate 10 files at once.

🔹 3. GUI Using Tkinter

Simple drag & drop interface.

🔹 4. Webhook + API

Turn it into a translation API service.

🔹 5. OCR for Images

Using pytesseract.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *