Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
jupyter-naas
GitHub Repository: jupyter-naas/awesome-notebooks
Path: blob/master/Deepl/Deepl_Translated_string_to_txt.ipynb
2973 views
Kernel: Python 3

Deepl.png

Deepl - Translated string to txt

Give Feedback | Bug report

Tags: #deepl #translate #text #txt #api #string

Last update: 2023-06-14 (Created: 2023-06-14)

Description: This notebook show how to translate a string with Deepl API and save it in a txt file.

Input

Import libraries

import naas try: import deepl except: !pip install deepl --user import deepl

Setup Variables

Get your authentification key

  • auth_key: The auth key is a unique identifier used for authentication and access to the DeepL API. It ensures that only authorized users can make API requests.

  • text: the text to be translated

  • target_lang: the language you wish to translate into, for more languages, go to references

  • output_file_path: the directory where you want to save the file if you are using Naas it will start with home/ftp/

auth_key = naas.secret.get("DEEPL_AUTH_KEY") or 'YOUR_DEEPL_AUTH_KEY' # Replace with your actual DeepL API authentication key text = ''' Announcing the Python client library for the DeepL API August 16, 2021 We’re excited to announce the release of our Python client library for the DeepL API. This is the first programming language-specific library we’ve built for the API, and our goal is to make it much easier for developers working with Python to build applications with DeepL. ''' target_lang = 'fr' # French output_file_path = "/home/ftp/file.txt"

Model

Translate the text

translator = deepl.Translator(auth_key) result = translator.translate_text(text, target_lang=target_lang) translated_text = result.text

Save translated text to a text file

with open(output_file_path, 'w', encoding='utf-8') as file: file.write(translated_text)

Output

Display result

print("Text file generated successfully here ->", output_file_path) print("------------------------------------------------------------") print("Result:") print(translated_text)