CloudConvert Python SDK

2.1.0 · active · verified Thu Apr 16

The `cloudconvert` library is the official Python SDK for the CloudConvert API v2, providing a convenient wrapper to convert, optimize, and manipulate various file formats programmatically. As of version 2.1.0, it supports features like Signed URLs and improved synchronous API endpoints for job and task waiting. The library is under active development, with major releases often coinciding with updates to the underlying CloudConvert API.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart example demonstrates how to configure the CloudConvert SDK, create a job to convert a file from a URL to PDF, wait for the conversion to complete, and then download the resulting PDF. It uses an environment variable for the API key for secure handling.

import cloudconvert
import os

# Configure CloudConvert with your API key from environment variable
# Get your API key at https://cloudconvert.com/dashboard/api/v2/keys
CLOUDCONVERT_API_KEY = os.environ.get('CLOUDCONVERT_API_KEY', 'YOUR_API_KEY')

if CLOUDCONVERT_API_KEY == 'YOUR_API_KEY' or not CLOUDCONVERT_API_KEY:
    print("Warning: CLOUDCONVERT_API_KEY not set. Using a placeholder. Set the environment variable or replace 'YOUR_API_KEY'.")
    # For demonstration, proceed with placeholder; in production, exit or raise error.
    # For actual usage, ensure a valid API key is present.

cloudconvert.configure(api_key=CLOUDCONVERT_API_KEY, sandbox=False)

try:
    # Create a job to convert a URL to PDF
    job = cloudconvert.Job.create(payload={
        "tasks": {
            'import-from-url': {
                'operation': 'import/url',
                'url': 'https://www.example.com/sample.html'
            },
            'convert-to-pdf': {
                'operation': 'convert',
                'input': ['import-from-url'],
                'output_format': 'pdf'
            },
            'export-to-url': {
                'operation': 'export/url',
                'input': ['convert-to-pdf']
            }
        }
    })

    # Wait for the job to complete
    job = cloudconvert.Job.wait(id=job['id'])

    # Download the output file
    for task in job["tasks"]:
        if task.get("name") == "export-to-url" and task.get("status") == "finished":
            export_task = task
            # Assuming a single file is exported for simplicity
            file = export_task["result"]["files"][0]
            
            # Create a 'downloads' directory if it doesn't exist
            os.makedirs('downloads', exist_ok=True)
            
            # Download the file to the 'downloads' directory
            download_path = os.path.join('downloads', file['filename'])
            cloudconvert.download(filename=download_path, url=file['url'])
            print(f"Successfully converted and downloaded to: {download_path}")
            break

except cloudconvert.exceptions.CloudConvertClientError as e:
    print(f"CloudConvert API Error: {e.message}")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →