You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Google翻译多PDF批量处理程序SSL证书验证失败问题求助

解决Google翻译API SSL证书验证失败问题

问题场景

开发批量PDF翻译工具时,公司内网环境下遭遇SSL证书链自签名验证失败错误,尝试多种常规禁用SSL验证的方法均无效,且无法更新SSL证书,需针对性解决方案。

错误信息

Exception has occurred: ConnectError
[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1002)
ssl.SSLCertVerificationError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1002)

During handling of the above exception, another exception occurred:

  File "C:\Users\외부PC\Desktop\PDF 번역 자동화\PDF 번역 자동화 프로그램.py", line 54, in translate_and_download_pdf
    translated_page = translator.translate(page_text, dest=target_language)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\외부PC\Desktop\PDF 번역 자동화\PDF 번역 자동화 프로그램.py", line 81, in <module>
    translate_and_download_pdf(file_paths, output_directory, target_language)
httpcore._exceptions.ConnectError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self signed certificate in certificate chain (_ssl.c:1002)

解决方案

googletrans v4+版本底层使用httpx而非requests发起请求,之前针对requests/urllib3的SSL设置完全无效,需直接为googletrans配置禁用SSL验证的httpx客户端。

修改步骤

  1. 导入httpx库,创建禁用SSL验证的客户端实例
  2. 初始化Translator时传入该客户端
  3. 清理代码中所有无效的SSL设置(避免冲突)

修改后的完整代码

import os
from tkinter import Tk
from tkinter.filedialog import askopenfilenames, askdirectory
from PyPDF2 import PdfReader, PdfWriter
from googletrans import Translator
import httpx

# 创建禁用SSL验证的httpx客户端
httpx_client = httpx.Client(verify=False)

def translate_and_download_pdf(file_paths, output_directory, target_language):
    # 传入自定义httpx客户端
    translator = Translator(client=httpx_client)

    for file_path in file_paths:
        file_name = os.path.basename(file_path)
        translated_text = ""

        with open(file_path, 'rb') as file:
            pdf = PdfReader(file)
            for page in pdf.pages:
                page_text = page.extract_text()
                if page_text:  # 跳过无文本的页面
                    translated_page = translator.translate(page_text, dest=target_language)
                    translated_text += translated_page.text + "\n\n"

        # 生成翻译后的PDF(修复原代码中合并文本的错误)
        translated_pdf = PdfWriter()
        # 添加A4尺寸空白页
        translated_pdf.add_blank_page(width=612, height=792)
        # 添加文本注释(简单实现,如需更美观排版建议使用ReportLab等专业库)
        from PyPDF2.generic import AnnotationBuilder
        annotation = AnnotationBuilder.free_text(
            translated_text,
            rect=(50, 50, 562, 742),
            font="Helvetica",
            font_size=12,
            font_color="black",
            border_color="white"
        )
        translated_pdf.add_annotation(page_number=0, annotation=annotation)

        output_file_path = os.path.join(output_directory, f"translated_{file_name}")
        with open(output_file_path, "wb") as output_file:
            translated_pdf.write(output_file)

        print(f"{file_name} 已翻译并保存至 {output_file_path}")

# 选择PDF文件
print("请选择需要翻译的PDF文件:")
Tk().withdraw()
file_paths = askopenfilenames(filetypes=[("PDF文件", "*.pdf")])

# 选择输出目录
print("请选择翻译文件保存目录:")
Tk().withdraw()
output_directory = askdirectory()

# 输入目标语言代码
target_language = input("请输入目标语言代码(例如:'zh-CN' 表示简体中文):")

# 执行翻译
if file_paths and output_directory:
    translate_and_download_pdf(file_paths, output_directory, target_language)
else:
    print("未选择文件或目录,程序退出")

关键说明

  • 核心修改:通过httpx.Client(verify=False)创建客户端并传给Translator,直接跳过SSL验证
  • 修复原代码错误:修正merge_text_file的误用(该方法需传入文本文件路径而非字符串)
  • 清理冗余代码:移除所有无效的requests/urllib3/ssl相关设置,避免冲突

内容的提问来源于stack exchange,提问作者omgflutt

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 03:50:13