Doctranslate.io Translation API
  • Introduction
    • Welcome to Doctranslate.io
    • What is DocTranslate.io
    • User Manual
  • Document Translation API
    • System requirement
    • Upload API
      • Upload multiple files and calculate usage cost
    • User API
      • User History
    • Translation API
      • API V1
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Image Translation
        • Audio Translation
        • Text Translation
        • Get result
      • API V2
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Image Translation
        • Audio Translation
        • Text Translation
        • Get result
      • API V3
        • Document Translation
        • Translate Multiple Files (currently supports document, video)
        • Audio Translation
        • Text Translation
        • Get result
    • Presentation Maker API
      • API V1
        • Presentation Maker
        • Presentation Maker For Multiple Files
      • API V2
        • Presentation Maker
        • Presentation Maker For Multiple Files
  • Presentation Video API
    • API V1
      • Create transcripts presentation
      • Update transcripts presentation
      • Create video presentation
      • Get result
    • API V2
      • Create transcripts presentation
      • Update transcripts presentation
      • Create video presentation
      • Get result
  • Others
    • Code examples
      • API V1
        • Upload Multiple Files
        • Translate Multiple Document Files
        • Text Translation
        • Document Translation
        • Image Translation
        • Audio Translation
        • Video Translation
        • Presentation Maker
        • Presentation Video
      • API V2
        • Upload Multiple Files
        • Translate Multiple Document Files
        • Text Translation
        • Document Translation
        • Image Translation
        • Audio Translation
        • Video Translation
        • Presentation Maker
        • Presentation Video
    • Support
Powered by GitBook
On this page
  1. Others
  2. Code examples
  3. API V1

Document Translation

import requests
import datetime
import time

# Constants for URLs and content type
BASE_URL = "https://doctranslate-api.doctranslate.io"
TRANSLATE_DOCUMENT_URL = f"{BASE_URL}/v1/translate/document"
GET_RESULT_ENDPOINT = f"{BASE_URL}/v1/result/"
CONTENT_TYPE = 'application/docx'

# Translation settings
original_language = None  # Automatically detect language if not specified
destination_language = 'ja'
process_mode = 'append'
translate_type = 'Professional'
publish_api = True

# File paths
input_file_path = '/data/example.docx'  # Input file path
output_folder_path = '/data/translated/'  # Output folder path

# Authentication token
auth_token = "<YOUR_API_TOKEN>"

# Corrected variable names according to their declaration at the top
form_data = {
    'file_type': CONTENT_TYPE,
    'original_lang': original_language,
    'dest_lang': destination_language,
    'process_mode': process_mode,
    'translate_type': translate_type,
    'bilingual_text_style__font': 'Helvetica',
    'bilingual_text_style__color': '#96d35f'
}

headers = {'Authorization': f'Bearer {auth_token}'}

# Corrected file upload part to match the expected key 'file' in the API
with open(input_file_path, 'rb') as file_to_translate:
    try:
        response = requests.post(TRANSLATE_DOCUMENT_URL, data=form_data, files={'file': file_to_translate}, headers=headers)
        response.raise_for_status()  # Proper error handling
        
        response_data = response.json()
        task_id = response_data.get('data', {}).get('task_id')
        
        if task_id:
            print(f'Task ID: {task_id}')
        else:
            print('Failed to get the task ID from the response.')
    except requests.exceptions.RequestException as e:
        print(f'An error occurred: {e}')

# Check for task_id before proceeding
if 'task_id' not in locals():
    print('Error: missing task_id')
    exit()

start_time = datetime.datetime.now()
url_download = ''

while True:
    if (datetime.datetime.now() - start_time).seconds >= 600:  # 10 minutes timeout
        print('Error: Processing timed out.')
        break

    try:
        response = requests.get(GET_RESULT_ENDPOINT + task_id, headers=headers)  # Use GET and append task_id to URL
        
        response_data = response.json()
        if response_data.get('status') == 'success' and response_data.get('data', {}).get('status') == 'done':
            url_download = response_data['data']['url_download']
            print(f'URL Download: {url_download}')
            break
    except requests.exceptions.RequestException as e:
        print(f'An error occurred while checking task status: {e}')
        break

    time.sleep(5)  # Wait before the next check to avoid flooding the server

if not url_download:
    print('Error: missing URL for download.')
PreviousText TranslationNextImage Translation

Last updated 1 year ago