Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert JPG to Word in Python

You can convert JPG/JPEG to Word DOCX programmatically on the cloud. In this article, you will learn how to convert JPG to Word document in Python using REST API.

The following topics are covered in this article:

  1. JPG/JPEG to Word Conversion REST API - Installation
  2. Convert JPG/JPEG to Word in Python using REST API
  3. JPG to Word Conversion without using Cloud Storage
  4. How to Convert JPEG to Word and Download Directly
# How to convert JPG to Word file in Python
# Create necessary API instances
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Define convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.storage_name = storage_name
settings.file_path = "python-testing/sample-file.jpg"
settings.format = "docx"
# DOCX convert options
settings.convert_options = groupdocs_conversion_cloud.DocxConvertOptions()
settings.output_path = "python-testing"
# Create convert document request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert JPG/JPEG to Word DOCX file
response = convert_api.convert_document(request)
print("Successfully converted JPG to Word document: " + str(response))
# How to Convert JPG/JPEG to Word Docx & Download Directly
# Create necessary API instances
import shutil
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert settings
settings = groupdocs_conversion_cloud.ConvertSettings()
settings.file_path = "python-testing/sample-file.jpg"
settings.format = "docx"
# output as document IOStream
settings.output_path = None
# Prepare convert request
request = groupdocs_conversion_cloud.ConvertDocumentRequest(settings)
# Convert JPG/JPEG to Word file Directly
response = convert_api.convert_document_download(request)
# Move downloaded file to your working directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# API initialization to download converted file
import shutil
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Create download file request
request = groupdocs_conversion_cloud.DownloadFileRequest("python-testing\\sample-file.docx", storage_name)
# Download converted file
response = file_api.download_file(request)
# Move the downloaded file to your directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# JPG/JPEG to Word Conversion without using cloud storage
# Create necessary API instances
import shutil
convert_api = groupdocs_conversion_cloud.ConvertApi.from_keys(client_id, client_secret)
# Prepare convert request
request = groupdocs_conversion_cloud.ConvertDocumentDirectRequest("docx", "H:\\groupdocs-cloud-data\\sample-file.jpg")
# Convert JPG to Docx without cloud storage
response = convert_api.convert_document_direct(request)
# Move downloaded file to your working directory
shutil.move(response, "H:\\groupdocs-cloud-data\\")
# Upload JPG/JPEG file to cloud storage
# Create an instance of the File API
file_api = groupdocs_conversion_cloud.FileApi.from_config(configuration)
# Call upload file request
request = groupdocs_conversion_cloud.UploadFileRequest("python-testing\sample-file.jpg", "H:\\groupdocs-cloud-data\\sample-file.jpg", storage_name)
# Upload pdf file to the cloud
response = file_api.upload_file(request)
print(response.uploaded)
# Import Python SDK in your python application from http://api.groupdocs.cloud
import groupdocs_conversion_cloud
# Get client_id and client_secret from https://dashboard.groupdocs.cloud after free registration.
client_id = "xxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
# Get File API configurations
configuration = groupdocs_conversion_cloud.Configuration(client_id, client_secret)
configuration.api_base_url = "https://api.groupdocs.cloud"
storage_name = "MyStorage"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment