python
pip install python-dotenv
pip install pdfplumber
pip install python-docx
pip install google-cloud-translate
尋找要啟用的API服務
Learn More →
啟用該服務
點「啟用」
申請憑證
取得憑證
將下載好的json檔搬移到專案目錄中
將.env檔的內容指到這個檔案
即可透過以下程式使用翻譯功能
from dotenv import load_dotenv
import pdfplumber
import docx
def get_pdf_content(filepath, page_number):
pdf = pdfplumber.open(filepath)
p = pdf.pages[page_number]
text = p.extract_text() #讀文字
# print(text)
return text
def translate_text_with_model(target, text, model="nmt"):
"""Translates text into the target language.
Make sure your project is allowlisted.
Target must be an ISO 639-1 language code.
See https://g.co/cloud/translate/v2/translate-reference#supported_languages
"""
from google.cloud import translate_v2 as translate
translate_client = translate.Client()
if isinstance(text, bytes):
text = text.decode("utf-8")
# Text can also be a sequence of strings, in which case this method
# will return a sequence of results for each text.
result = translate_client.translate(text, target_language=target, model=model)
# print(u"Text: {}".format(result["input"]))
# print(u"Translation: {}".format(result["translatedText"]))
# print(u"Detected source language: {}".format(result["detectedSourceLanguage"]))
return result["translatedText"]
def write_docx(filepath, text):
mydoc = docx.Document()
first_para = mydoc.add_paragraph("第一章 aaaa\n")
first_para.add_run(text)
mydoc.save(filepath)
if __name__ == '__main__':
load_dotenv()
source_filepath=r"/home/amos/文件/kivy.pdf" # 外文pdf檔路徑
target_filepath=r"./test.docx" # 翻譯好的word檔路徑
source = 'en' # 原始檔案語系
target = 'zh-TW' # 要翻譯為哪個語系
page_number = 14 # 取pdf檔第幾頁內容
text = get_pdf_content(source_filepath, page_number)
target_text = translate_text_with_model(target, text)
# print(target_text)
write_docx(target_filepath, target_text)
"tavily-mcp": { "command": "cmd", "args": ["/c", "npx", "-y", "tavily-mcp@0.1.4"], "env": { "TAVILY_API_KEY": "" }, "disabled": false, "autoApprove": [] }
May 16, 2025將這段程式碼儲存為 setup_numlock.sh,並賦予執行權限:
Nov 6, 2024在 Ubuntu 中,如果你想停止網路連接失敗的訊息彈出通知,可以嘗試以下幾個方法:
Nov 5, 2024連到google網站 點選google雲端硬碟 打開「上傳三清官網」資料夾 建立活動資料夾 上傳活動照片
Oct 21, 2022or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up