How to Convert JPG to Word in Python

How to Convert JPG to Word in Python

JPG, also referred to as JPEG, stands as a highly prevalent compressed image format primarily used to store digital images. It reigns as the foremost choice for images featuring lossy compression. Conversely, Microsoft Word serves as the premier tool for word processing and document creation. In specific scenarios, the conversion of JPG to MS Word becomes imperative to facilitate editing. A JPG to Word converter expeditiously transforms images into doc files in mere seconds. Consequently, this article will instruct you on how to perform JPG to Word conversion using Python.

The following topics shall be covered in this article:

JPG/JPEG to Word Conversion REST API - Installation {#JPG/JPEG-to-Word-Conversion-REST-API—Installation}

For converting JPG to Word document, I will be using the Python SDK of GroupDocs.Conversion Cloud API. This API allows you to convert your documents and images of any supported file format to any format you need. You can easily convert between over 50 types of documents and images such as Word, PowerPoint, Excel, PDF, HTML, CAD, raster images, etc.

You can install GroupDocs.Conversion Cloud to your Python application using the following command in the console:

pip install groupdocs_conversion_cloud

Please get your Client ID and Client Secret from the dashboard before you start following the steps and available code examples. Once you have your ID and secret, please add in the code as shown below:

# 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"

Convert JPG/JPEG to Word in Python using REST API

You can convert JPG to Word file by following the simple steps mentioned below:

Upload the Document

Firstly, upload the Word document to the cloud using the code example given below:

# 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)

As a result, the uploaded Word file will be available in the files section of your dashboard on the cloud.

Convert JPG to Word in Python

You can convert JPG file to Word programmatically by following the steps given below:

  • Create an instance of ConvertApi
  • Create an instance of the ConvertSettings
  • Set the JPG file path
  • Assign “docx” to the format
  • Define DocxConvertOptions if required
  • Provide the output file path
  • Create ConvertDocumentRequest with ConvertSettings
  • Get results by calling the convert_document() method with ConvertDocumentRequest

The following code example shows how to convert JPG to Word without losing format using REST API:

# 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))
Convert JPG to Word in Python

How to Convert JPG to Word in Python

Download the Converted File

The above code sample will save the converted docx file on the cloud. You can download it using the following code sample:

# 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 to Word Conversion without using Cloud Storage

You can convert JPG to Word document without using cloud storage by following the steps given below:

  • Create an instance of the ConvertApi
  • Create ConvertDocumentDirectRequest and pass requested document format and the input file path
  • Get results by calling the convert_document_direct_()_ method with ConvertDocumentDirectRequest
  • Optionally, save the output file to the local path using shutil.move() method

The following code example shows how to convert JPG to Word without using cloud storage:

# 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\\")

How to Convert JPG to Word and Download Directly

You can easily convert JPG to Word file programmatically by following the steps given below:

  • Create an instance of ConvertApi
  • Create an instance of the ConvertSettings
  • Set the Word file path
  • Assign “docx” to the format
  • Set “None” to the output path
  • Create ConvertDocumentRequest with ConvertSettings
  • Get results by calling the convert_document_download() method
  • Optionally, save the output file to the local path using shutil.move() method

The following code example shows how to convert JPG to Word and download it directly using a REST API in Python:

# 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\\")

The API shall return the converted word document in response. Please follow the steps mentioned earlier to upload a file.

Online JPG to Word Converter

How to convert jpg to word online? Please try the following free JPG to Word online conversion tool to convert jpg to editable docx, which is developed using the above API.

Conclusion

In this article, you have learned:

  • how to convert JPG to Word file using Python on the cloud;
  • upload the JPG file to the cloud and then download the converted docx file from the cloud;
  • how to convert JPG to Word without using cloud storage programmatically;
  • how to convert JPG to Word file and download it directly;

You can learn more about GroupDocs.Conversion Cloud API using the documentation. We also provide an API Reference section that lets you visualize and interact with our APIs directly through the browser.

Ask a question

If you have any questions about JPG to Word converter, please feel free to ask us on the Free Support Forum.

FAQs

How to convert JPG to Word Docx?

Install this Python library to convert JPG/JPEG to Word in Python programmatically. You can visit the documentation for complete API details.

What is the fastest way to convert JPG to Word?

Online JPG to DOC Converter works very fast and you can change JPG to DOC in a few seconds.

How can I convert image to word online for free?

  • Open our free JPG to DOC converter.
  • Click inside the file drop area to upload JPG file or drag & drop JPG file.
  • Click on Convert button. Your JPG files will be uploaded and converted to DOC format.
  • Download link of output files will be available instantly after conversion.

Is it safe to convert JPG to DOC using free online converter?

Yes, no one has access to your uploaded files and we delete uploaded files after 24 hours.

See Also

We recommend you to visit the following articles to learn about: