本文目录导读:

我来介绍几种批量翻译文档的脚本方法,从简单到专业:
使用免费API(适合小批量)
Python + Google翻译(无需API密钥)
from googletrans import Translator
import os
from pathlib import Path
def batch_translate_folder(input_folder, output_folder, src_lang='auto', dest_lang='zh-cn'):
translator = Translator()
# 创建输出文件夹
Path(output_folder).mkdir(parents=True, exist_ok=True)
# 遍历所有txt文件
for file_path in Path(input_folder).glob('*.txt'):
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 翻译内容(注意:有长度限制)
translated = translator.translate(content, src=src_lang, dest=dest_lang)
# 保存翻译结果
output_path = Path(output_folder) / f"translated_{file_path.name}"
with open(output_path, 'w', encoding='utf-8') as f:
f.write(translated.text)
print(f"已翻译: {file_path.name}")
# 使用示例
batch_translate_folder('input_docs', 'output_docs')
使用DeepL API(更专业,需付费)
import deepl
import os
def translate_with_deepl():
# 注册获取API密钥:https://www.deepl.com/pro-api
auth_key = "你的API密钥"
translator = deepl.Translator(auth_key)
input_dir = "documents"
output_dir = "translated_documents"
for filename in os.listdir(input_dir):
if filename.endswith(('.txt', '.docx', '.pdf')):
input_path = os.path.join(input_dir, filename)
# 读取文件
with open(input_path, 'r', encoding='utf-8') as f:
text = f.read()
# 翻译(支持批量处理)
result = translator.translate_text(
text,
source_lang="EN",
target_lang="ZH"
)
# 保存结果
output_path = os.path.join(output_dir, f"zh_{filename}")
with open(output_path, 'w', encoding='utf-8') as f:
f.write(result.text)
# 使用示例
translate_with_deepl()
处理多种文档格式(Word, PDF等)
from docx import Document
import PyPDF2
from googletrans import Translator
class MultiFormatTranslator:
def __init__(self):
self.translator = Translator()
def translate_word(self, input_path, output_path, target_lang='zh-cn'):
doc = Document(input_path)
for paragraph in doc.paragraphs:
if paragraph.text.strip():
try:
translated = self.translator.translate(
paragraph.text,
dest=target_lang
)
paragraph.text = translated.text
except:
print(f"警告: 无法翻译段落: {paragraph.text[:50]}...")
doc.save(output_path)
def translate_pdf(self, input_path, output_path, target_lang='zh-cn'):
# 读取PDF
with open(input_path, 'rb') as file:
pdf_reader = PyPDF2.PdfReader(file)
text = ""
for page in pdf_reader.pages:
text += page.extract_text()
# 分段翻译(避免超长限制)
chunks = [text[i:i+5000] for i in range(0, len(text), 5000)]
translated_chunks = []
for chunk in chunks:
translated = self.translator.translate(chunk, dest=target_lang)
translated_chunks.append(translated.text)
# 保存为txt
with open(output_path.replace('.pdf', '.txt'), 'w', encoding='utf-8') as f:
f.write(''.join(translated_chunks))
# 使用示例
translator = MultiFormatTranslator()
translator.translate_word('input.docx', 'output.docx')
translator.translate_pdf('input.pdf', 'output.pdf')
批量处理 + 进度显示
import os
from concurrent.futures import ThreadPoolExecutor, as_completed
from tqdm import tqdm
from googletrans import Translator
def translate_file(file_path, output_dir, target_lang='zh-cn'):
translator = Translator()
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
# 翻译
result = translator.translate(content, dest=target_lang)
# 保存
output_path = os.path.join(output_dir, f"translated_{os.path.basename(file_path)}")
with open(output_path, 'w', encoding='utf-8') as f:
f.write(result.text)
return True, file_path
except Exception as e:
return False, f"{file_path}: {str(e)}"
def batch_translate_parallel(input_dir, output_dir, max_workers=5):
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
# 获取所有txt文件
files = [os.path.join(input_dir, f) for f in os.listdir(input_dir)
if f.endswith('.txt')]
# 多线程并行处理
with ThreadPoolExecutor(max_workers=max_workers) as executor:
futures = {executor.submit(translate_file, file, output_dir): file
for file in files}
# 显示进度条
for future in tqdm(as_completed(futures), total=len(files), desc="翻译进度"):
success, msg = future.result()
if not success:
print(f"失败: {msg}")
# 使用示例
batch_translate_parallel('input_folder', 'output_folder')
使用命令行工具(最简单)
安装 trans 工具
# 安装
pip install translate-shell
# 批量翻译所有txt文件
for file in *.txt; do
trans :zh "$file" > "translated_$file"
done
注意事项
- API限制:免费API有每日使用限额
- 大文件处理:建议分段处理,每段不超过5000字符
- 格式保持:Word/PDF可能丢失格式
- 质量验证:建议抽样检查翻译质量
推荐方案
- 小批量免费:使用Google翻译(googletrans库)
- 专业商用:使用DeepL API或百度翻译API
- 大量处理:使用Ray或Dask分布式处理框架
- GUI工具:可以使用OmegaT或Crowdin等专业翻译工具
需要我帮你实现特定格式文档(如Word、Excel、PDF)的批量翻译脚本吗?
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播